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
Huma
Community Leader
-
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
- Smartsheet Customer Resources
- 64.2K Get Help
- 419 Global Discussions
- 221 Industry Talk
- 461 Announcements
- 4.8K Ideas & Feature Requests
- 143 Brandfolder
- 141 Just for fun
- 58 Community Job Board
- 462 Show & Tell
- 32 Member Spotlight
- 1 SmartStories
- 299 Events
- 38 Webinars
- 7.3K Forum Archives