Click here to Skip to main content
14,494,016 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi all,
i am getting an unsupported media type error and error code is 415 when i am trying to send a pdf file using api in c#. i am using below code. can some one suggest whats going on wrong here...

string url = "My URL";



               var method = new HttpMethod("POST");
               HttpClient client = new HttpClient();
               //client.DefaultRequestHeaders.Add("ContentType", "application/json");
               client.DefaultRequestHeaders.Add("ContentType", "JSON");
               client.DefaultRequestHeaders.Add("Authorization", "Bearer CODE");
               MultipartFormDataContent form = new MultipartFormDataContent();
               HttpContent content = new StringContent("fileToUpload");
               form.Add(content, "fileToUpload");

               var stream = new FileStream("G:\\API response\\CA_checkAPiResponses\\fileFolder\\Test.pdf", FileMode.Open);
               content = new StreamContent(stream);

               Dictionary<string, string> parameters = new Dictionary<string, string>();
               parameters.Add("fileContents", "benemanuel");
               HttpContent DictionaryItems = new FormUrlEncodedContent(parameters);
               form.Add(DictionaryItems, "medicineOrder");

               content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") //  //form-data
               {
                   FileName = "Test.pdf"

               };
               form.Add(content);
               HttpResponseMessage response = (client.PostAsync(url, form)).Result;

               if (response.IsSuccessStatusCode)
               {
                   Response.Write("API is working  <br/>");

               }
               else
               {
                   Response.Write("API is Not Working currently <br/>");
               }
           }
           catch (Exception ex)
           {
               Response.Write("Some error occur.  <br/>");
           }


What I have tried:

I have tried below solution-

string url = "My URL";



               var method = new HttpMethod("POST");
               HttpClient client = new HttpClient();
               //client.DefaultRequestHeaders.Add("ContentType", "application/json");
               client.DefaultRequestHeaders.Add("ContentType", "JSON");
               client.DefaultRequestHeaders.Add("Authorization", "Bearer CODE");
               MultipartFormDataContent form = new MultipartFormDataContent();
               HttpContent content = new StringContent("fileToUpload");
               form.Add(content, "fileToUpload");

               var stream = new FileStream("G:\\API response\\CA_checkAPiResponses\\fileFolder\\Test.pdf", FileMode.Open);
               content = new StreamContent(stream);

               Dictionary<string, string> parameters = new Dictionary<string, string>();
               parameters.Add("fileContents", "benemanuel");
               HttpContent DictionaryItems = new FormUrlEncodedContent(parameters);
               form.Add(DictionaryItems, "medicineOrder");

               content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") //  //form-data
               {
                   FileName = "Test.pdf"

               };
               form.Add(content);
               HttpResponseMessage response = (client.PostAsync(url, form)).Result;

               if (response.IsSuccessStatusCode)
               {
                   Response.Write("API is working  <br/>");

               }
               else
               {
                   Response.Write("API is Not Working currently <br/>");
               }
           }
           catch (Exception ex)
           {
               Response.Write("Some error occur.  <br/>");
           }
Posted
Updated 27-Mar-20 3:11am
Comments
Richard MacCutchan 26-Mar-20 10:14am
   
Could it have something to do with the fact that you specify the ContentType as JSON?
phil.o 26-Mar-20 10:43am
   
I was going to emit a second wild guess, but I'll leave this one to you.
Virtually 5'd.
Richard MacCutchan 26-Mar-20 10:49am
   
:)
Gaurav Dixit 27-Mar-20 7:31am
   
then what else i can keep gere in content type ?
Richard MacCutchan 27-Mar-20 8:38am
   
What do you mean? You are trying to upload a PDF file, so why are you telling the loader that it is JSON?
Gaurav Dixit 27-Mar-20 9:03am
   
i tried commenting content type. but still getting same error. What content type i should use for uploading pdf?

i googled a lit bit some post suggesting that below line of my code can create a problem-

content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")

Richard MacCutchan 27-Mar-20 9:12am
   
See below.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Use the correct mime type as listed in MIME types (IANA media types) - HTTP | MDN[^].
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100