b_J
b_J

Reputation: 125

Read response after uploading attachment using Web API

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

Upvotes: 1

Views: 494

Answers (1)

Mohammad Nikravesh
Mohammad Nikravesh

Reputation: 975

Seems you need to add the following line in your http header request on the client side (where you are sending the rest request to your web-api).

Hope this helps.

Content-Type: application/json

Upvotes: 0

Related Questions