Can't import a certificate via XML API using C#

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can't import a certificate via XML API using C#

L0 Member

Hello,

 

I'm trying to import a certificate to a Palo Alto VM-50 via XML API with an App written in C# but I always get this error:

 

<response status = 'error' code = '400'><result><msg>No file uploaded</msg></result></response>

 

 

My C# code is below, I can't see where the problem is, if I use this curl command the file gets uploaded successfully:

 

curl --insecure --header "Authorization:Basic myauth" -F "file=@testCA.crt" "https://10.16.66.229/api?type=import&category=certificate&certificate-name=TestCA&format=pem"

 

 

Any ideas?

 

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace TestUploadPA
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            string url = "https://10.16.66.229/api/?type=import&category=certificate&certificate-name=TestCA&format=pem";
            string filePath = "testCA.crt";
            string username = "myuser";
            string password = "mypass";

            var handler = new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true,
            };

            using (HttpClient client = new HttpClient(handler))
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password)));

                using (var formData = new MultipartFormDataContent())
                using (var fileContent = new ByteArrayContent(File.ReadAllBytes(filePath)))
                {
                    fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    formData.Add(fileContent, "file", Path.GetFileName(filePath));

                    HttpResponseMessage response = await client.PostAsync(url, formData);

                    if (response.IsSuccessStatusCode)
                    {
                        string content = await response.Content.ReadAsStringAsync();
                        Console.WriteLine($"XML result: {content}");
                    }
                    else
                    {
                        Console.WriteLine($"Error uploading file. Status code: {response.StatusCode}");
                    }
                }
            }
        }
    }
}

 

 

1 accepted solution

Accepted Solutions

L0 Member

Hello,

I managed to figured out what was wrong: the boundary in the multipart/form-data content-type header was being double quoted. I added some code to remove the quotes and I was able to import the certificate to the Palo Alto firewall successfully.

View solution in original post

1 REPLY 1

L0 Member

Hello,

I managed to figured out what was wrong: the boundary in the multipart/form-data content-type header was being double quoted. I added some code to remove the quotes and I was able to import the certificate to the Palo Alto firewall successfully.

  • 1 accepted solution
  • 901 Views
  • 1 replies
  • 0 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!