Getting Bad Request 400 error when uploading attachment through API call.

Options

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

  • Humashankar
    Humashankar ✭✭✭✭✭
    Options

    Try headers like Authorization, Content-Type, and Accept to triage this 

    Huma

    Community Leader

  • ramprosad
    Options

    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              

       }

     }



  • Brian_Richardson
    Brian_Richardson Overachievers
    Options

    I'm not super familiar with C#. But it looks like you're sending ContentType instead of Content-Type as the header.

    BRIAN RICHARDSON | PMO TOOLS AND RESOURCES | HE|HIM

    SEATTLE WA, USA

    IRON MOUNTAIN