0

Command I need to convert :

curl.exe --digest -u login:pw -s -F "cert=@.\cert.pem" http://127.0.0.1/upload.htm

C# code I'm trying :

 HttpClientHandler handler = new HttpClientHandler();
            handler.Credentials = new NetworkCredential("login", "pw");
            HttpClient client = new HttpClient(handler);
            client.BaseAddress = new Uri("http://127.0.0.1");
...
 var content = new StringContent(fileContents);      
            content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = "cert",
                FileName = "cert.pem"
            };


            await client.PostAsync("/upload.htm", content);

Result :

<body><h1>HTTP/1.0 415 Unsupported Media Type</h1></body>

aldo tested the following c# code :

string executableLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            String path = Path.Combine(executableLocation, "cert.pem");
            var fs = File.Open(path, FileMode.Open);                      

            var multiPartContent = new MultipartFormDataContent();
            var fc = new StreamContent(fs);
            fc.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

            multiPartContent.Add(fc, "certUsageUnspecified", "cert.pem");                

            var uploadCertificate = await client.PostAsync("/upload.htm", multiPartContent);               
            
            logger.Info(await uploadCertificate.Content.ReadAsStringAsync());

            logger.Info("=== end upload certificate ===");

and result is the following :

<body><h1>HTTP/1.0 400 Bad Request</h1></body>

I don't know what I'm doing wrong there but I can't find the solution. It's working fine with the curl command.

4
  • Maybe this will help you: stackoverflow.com/questions/5152723/… Commented Oct 27, 2021 at 15:48
  • @Fildor unfortunately, I can't use wireshark. I have to test on a server to acceed the IP and I don't any dev tools on it. Commented Oct 27, 2021 at 16:00
  • @Auditive it's not using httpclient :( Commented Oct 27, 2021 at 16:01
  • 1
    You need to understand what that curl command does first. There are several problems and contradictions. Content-Disposition is a response header. The code posts a JSON body when the curl command performs a FORM POST, hence the different media types. curl uses digest authentication, it doesn't send a username/password combination. Commented Oct 27, 2021 at 16:01

1 Answer 1

0

curl -Fis for multipart -F, --form <name=content> Specify HTTP multipart POST data Try the following:

 var content = new MultipartFormDataContent();
 var fs = File.Open(".\cert.pem", FileMode.Open);
 var fc = new StreamContent(fs);
 fc.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
 content.Add(fc, "cert", "cert.pem");      

For information only curl headers received with a file cert.pem whose content is "cert".

POST
Partial body: --------------------------87b709a918c0166a
Content-Disposition: form-data; name="cert"; filename="cert.pem"
Content-Type: application/octet-stream

"cert"

--------------------------87b709a918c0166a--

Body: --------------------------87b709a918c0166a
Content-Disposition: form-data; name="cert"; filename="cert.pem"
Content-Type: application/octet-stream

"cert"

--------------------------87b709a918c0166a--

The equivalent received with proposed .NET code:

POST
Partial body: --7ee9988d-bfd9-46d6-b0c9-74af30d7a6a2
Content-Disposition: form-data; name=cert; filename=cert.pem; filename*=utf-8''cert.pem
Content-Type: application/octet-stream

"cert"

--7ee9988d-bfd9-46d6-b0c9-74af30d7a6a2--

Body: --7ee9988d-bfd9-46d6-b0c9-74af30d7a6a2
Content-Disposition: form-data; name=cert; filename=cert.pem; filename*=utf-8''cert.pem
Content-Type: application/octet-stream

"cert"

--7ee9988d-bfd9-46d6-b0c9-74af30d7a6a2--

This is not strictly the same for Content-Disposition: header value but its very close.

Sign up to request clarification or add additional context in comments.

4 Comments

I already tried and I always receive a : "400 Bad request".
I've tested using a simple web node server with your curl command. Response from the server include a Transfer-Encoding: chunked Then curl send the a multipart as suggested. But the content is sent as Content-Type: application/octet-stream. So you should probably change the code to use a StreamContent. I'm going to update my answer. Results between curl and my tests are better.
Hello, I adapted the code as suggested but still the same error. 400 Bad request.
At this stage you should probably perform a network trace on your computer to compare curl and .net. This may help a lot.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.