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", "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")
{
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", "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")
{
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/>");
}