Getting Bad Request 400 error when uploading attachment through API call.
I am getting Bad Request 400 error when calling API to upload attachment in C#. However, Postman call is success. Thanks in advance for any help.
Answers
-
Try headers like Authorization, Content-Type, and Accept to triage this
-
Hello Humashankar, thanks for your advice. I tried passing everything what post man is carrying over, still no luck.
using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, url))
byte[] bytestream = File.ReadAllBytes("c:\\rams\\abc.pdf");
using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, url))
{
requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
requestMessage.Headers.Add("Host", "api.smartsheet.com");
requestMessage.Headers.Add("Accept", "*/*");
requestMessage.Headers.Add("ccept-Encoding", "gzip,deflate,br");
var form = new MultipartFormDataContent();
var fileContent = new System.Net.Http.ByteArrayContent(bytestream);
fileContent.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data;boundary=--------------------------024810352705804951850594");
fileContent.Headers.ContentLength = 1057403;
fileContent.ReadAsStream();
form.Add(fileContent, "file", "abc.pdf");
requestMessage.Content = form;
try
{
var response = StaticHttpClient.getHttpClient().SendAsync(requestMessage).Result;
response.EnsureSuccessStatusCode();
var responseContent = response.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
// log
}
}
-
I'm not super familiar with C#. But it looks like you're sending ContentType instead of Content-Type as the header.
Categories
- All Categories
- 14 Welcome to the Community
- Customer Resources
- 64.9K Get Help
- 439 Global Discussions
- 138 Industry Talk
- 471 Announcements
- 4.9K Ideas & Feature Requests
- 129 Brandfolder
- 148 Just for fun
- 68 Community Job Board
- 488 Show & Tell
- 33 Member Spotlight
- 2 SmartStories
- 300 Events
- 36 Webinars
- 7.3K Forum Archives