Click here to Skip to main content
15,947,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

1 solution

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900