So, I have a POST request to upload a file. I'm able to do that request inside a postman with simple settings like this: Body: form-data, body has only one item. Key=file, Value=xxxx.pdf
No authorization. Final working request from postman console looks like this:
Request Headers
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Cache-Control: no-cache
Postman-Token: 8d5df709-8f9e-48e2-bf20-f300b24d4be8
Host: api.xxxx.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------138420394858496796018969
Cookie: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Content-Length: 976797
Request Body
file: undefined
This works and file gets uploaded. But when I do the same thing in .net Core, it fails every time (400 - bad request)
using (var message = new HttpRequestMessage(HttpMethod.Post, uploadUri))
{
using (var fileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read))
using (var formDataContent = new MultipartFormDataContent())
{
using (var fileContent = new StreamContent(fileStream))
{
formDataContent.Add(fileContent, "file");
message.Content = formDataContent;
using (var response = await httpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead))
{
response.EnsureSuccessStatusCode();
}
}
}
}
Edit: Only difference that I can see is the content-disposition header being added to StreamContent. Hovewer I was not able to remove this header.
Edit: After a talk with the developer of the API, the problem is 100% the body & headers of the request. Api does not want a content-disposition header and excpects body multipart/form-data with pair file=byteArray
CodePudding user response:
I do believe you're missing Content-Type header value set:
fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("image/png");
I've tested with a *.jpg file and it works OK:
using (var multipartFormContent = new MultipartFormDataContent())
{
//Load the file and set the file's Content-Type header
var fileStreamContent = new StreamContent(File.OpenRead(filePath));
fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("image/png");
//Add the file to form
multipartFormContent.Add(fileStreamContent, name: "file", fileName: filePath);
//Send it
var response = await new HttpClient().PostAsync("http://localhost:5000/[Controller]/[Action]", multipartFormContent);
response.EnsureSuccessStatusCode();
}
CodePudding user response:
Might be security issue, try to add this before HTTP request code
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;