I am trying to create an attachment using the Support Bee API as documented here: https://supportbee.com/api#create_attachment
I have written a service that uses an HttpClient
to create and send the request using a filename.
If I test in in Postman, it succeeds. I am using form-data
for the body and just selecting the file to upload from the UI:
It doesn't work when I try to upload it via my HttpClient
Service:
public async Task<string> CreateAttachmentAsync(string fileName)
{
// "client" is HttpClient provided via D.I.
MultipartFormDataContent content = new MultipartFormDataContent();
content.Add(new StreamContent(new FileStream(fileName, FileMode.Open)), "files[]");
using (HttpResponseMessage response = await client.PostAsync(
"https://xxx.supportbee.com/attachments?auth_token=xxx",
content))
{
string responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
}
This results in a 500 Internal Server Error. Inspecting the MultipartFormDataContent
object I can see that it's header values are automatically being set:
{ Content-Type: multipart/form-data; boundary="c9be3778-4de5-4460-9929-adcaa6bdda79" Content-Length: 164 }
I have also tried reading the file to a byte array first and using ByteArrayContent
instead of StreamContent
to no avail. The response doesn't provide anything helpful, but since the request works in Postman I must have something wrong with my code, but I don't know what else to try.
Edit: I tested with Fiddler to compare the successful Postman request to my code. Here is the request with Postman:
POST https://xxx.supportbee.com/attachments?auth_token=xxx HTTP/1.1 User-Agent: PostmanRuntime/7.22.0 Accept: / Cache-Control: no-cache Postman-Token: f84d22fa-b4b1-4bf5-b183-916a786c6385 Host: xx.supportbee.com Content-Type: multipart/form-data; boundary=--------------------------714700821471353664787346 Accept-Encoding: gzip, deflate, br Content-Length: 241 Connection: close
----------------------------714700821471353664787346 Content-Disposition: form-data; name="files[]"; filename="sample.txt" Content-Type: text/plain
This contains example text. ----------------------------714700821471353664787346--
And the failing request from my code:
POST https://xxx.supportbee.com/attachments?auth_token=xxx HTTP/1.1 Host: xxx.supportbee.com Accept: / Accept-Encoding: gzip, deflate, br Connection: close Content-Type: multipart/form-data; boundary="ea97cbc1-70ea-4cc4-9801-09f5feffc763" Content-Length: 206
--ea97cbc1-70ea-4cc4-9801-09f5feffc763 Content-Disposition: form-data; name="files[]"; filename=sample; filename*=utf-8''sample
This contains example text. --ea97cbc1-70ea-4cc4-9801-09f5feffc763--
The difference I can see is that the individual part in Postman has its own Content-Type: text/plain
header for the file, and mine doesn't. I'm unable to add this because if I try content.Headers.Add("Content-Type", "text/plain");
It fails with 'Cannot add value because header 'Content-Type' does not support multiple values.'