- Access exclusive content
- Connect with peers
- Share your expertise
- Find support resources
04-15-2024 04:56 PM - edited 04-15-2024 04:57 PM
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}");
}
}
}
}
}
}
04-22-2024 09:38 AM
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.
04-22-2024 09:38 AM
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.
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!