locked
Read response after uploading attachment using web API c# console App RRS feed

  • Question

  • User42464830 posted

    I am trying to read response after uploading an attachment using web API c#. Getting below error:

    no mediaTypeFormatter is available to read an object of type 'String' from content with media type 'multipart/form-data.

    Here is my part of code:

    string filepath = "C:/Users/O42895/Desktop/DownloadAttachmen20189t.jpg";

                  string filename = "DownloadAttachmen20189t.jpg";

     

                  MultipartFormDataContent content = new MultipartFormDataContent();

                  ByteArrayContent fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(filepath));

                  fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = filename };

                  content.Add(fileContent);

                   

                  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));

     

     

                  response = await client.PostAsync("https:--.com/flex/upload/document/genericattachment", content);


                  
    var resBody = response.Content.ReadAsStringAsync().Result;
                      Console.WriteLine(resBody);

    I am getting the response but unable to print or read. Please let me know if any one knows

    Wednesday, August 22, 2018 12:48 PM

All replies

  • User475983607 posted

    The error message indicates logical errors in the upload process where you are trying to upload a file as a string.  There is not enough code to even guess where your logic error is.

    Is there any way you can post the action and client code?

    Wednesday, August 22, 2018 1:03 PM
  • User-474980206 posted

    the error means the return type content type is 'multipart/form-data'. while this is a common post form format, its not a common response format. your resBody should be a string of mime-type attachments.  should look something like:

    --WDCovLhyTgyWCrIdos2autXLLk8Qf8Pd
    Content-Dis-data; name="table_name"
    
    incident
    --WDCovLhyTgyWCrIdos2autXLLk8Qf8Pd
    Content-Dis-data; name="table_sys_id"
    
    efa6bb01cb30020035c7b967634c9c4c
    --WDCovLhyTgyWCrIdos2autXLLk8Qf8Pd
    Content-Dis-data; name="f"; filename="sample.txt"
    Content-Type: text/plain
    
    test test test test test
    
    --WDCovLhyTgyWCrIdos2autXLLk8Qf8Pd--

    Wednesday, August 22, 2018 3:45 PM
  • User42464830 posted

    Hi,

    I checked and found that response is empty. 

    Full code is below. pls let me know if u have any idea on this.

    string filepath = "C:/Users/O42895/Desktop/DownloadAttachmen20189t.jpg";

                  string filename = "DownloadAttachmen20189t.jpg";

     

                  MultipartFormDataContent content = new MultipartFormDataContent();

                  ByteArrayContent fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(filepath));

                  fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = filename };

                  content.Add(fileContent);

                   

                  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));

     

                  response = await client.PostAsync("https://---.com/flex/upload/document/genericattachment", content);

    var resBody = response.Content.ReadAsStringAsync().Result;
                      Console.WriteLine(resBody);
    </div> </div>

    Thursday, August 23, 2018 6:54 AM
  • User-474980206 posted
    Probable the response is only status code 200 and no content. Check the api docs or source for the return status and any response content.
    Friday, August 24, 2018 12:24 AM
  • User42464830 posted

    Thanks for your reply. Can you please elaborate your answer. I am unable to understand.

    Friday, August 24, 2018 6:36 AM
  • User-474980206 posted

    if the upload is done via a REST api, a common response is just the status header. in webapi this would be:

    return new HttpResponseMessage(HttpStatusCode.OK);

    there is no content.

    Friday, August 24, 2018 4:25 PM