Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting (415) Unsupported Media Type error

What I have to do is that I have to post JSON data in given URL Where my JSON looks like

{
    "trip_title":"My Hotel Booking",
    "traveler_info":{
        "first_name":"Edward",
        "middle_name":"",
        "last_name":"Cullen",
        "phone":{
            "country_code":"1",
            "area_code":"425",
            "number":"6795089"
        },
        "email":"[email protected]"
    },
    "billing_info":{
        "credit_card":{
            "card_number":"47135821",
            "card_type":"Visa",
            "card_security_code":"123",
            "expiration_month":"09",
            "expiration_year":"2017"
        },
        "first_name":"Edward",
        "last_name":"Cullen",
        "billing_address":{
            "street1":"Expedia Inc",
            "street2":"108th Ave NE",
            "suite":"333",
            "city":"Bellevue",
            "state":"WA",
            "country":"USA",
            "zipcode":"98004"
        },
        "phone":{
            "country_code":"1",
            "area_code":"425",
            "number":"782"
        }
    },
    "marketing_code":""
}

And my function

string message = "URL";
_body="JSON DATA";
HttpWebRequest request = HttpWebRequest.Create(message) as HttpWebRequest;
if (!string.IsNullOrEmpty(_body))
{
    request.ContentType =  "text/json";
    request.Method =  "POST";

    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
    {
        streamWriter.Write(_body);
        streamWriter.Flush();
        streamWriter.Close();
    }
}

using (HttpWebResponse webresponse = request.GetResponse() as HttpWebResponse)
{
    using (StreamReader reader = new StreamReader(webresponse.GetResponseStream()))
    {
        string response = reader.ReadToEnd();
    }
}

And when I am posting it; I am getting an error

"The remote server returned an error: (415) Unsupported Media Type."

Anybody have idea about it; where I am mistaking?

like image 643
user1785373 Avatar asked Oct 30 '12 13:10

user1785373


People also ask

How do I fix 415 unsupported media type?

Fixing 415 Unsupported Media Type errorsEnsure that you are sending the proper Content-Type header value. Verify that your server is able to process the value defined in the Content-Type header. Check the Accept header to verify what the server is actually willing to process.

What does unsupported media type mean in Postman?

Http 415 Media Unsupported is responded back only when the content type header you are providing is not supported by the application. With POSTMAN, the Content-type header you are sending is Content type 'multipart/form-data not application/json .

How do I change media type in Postman?

To do this, open Postman and create a new request by selecting New->Request from the top left: Under Headers, select Key = Content-Type: For Value, select application/json: THANKS FOR READING.


3 Answers

Try this:

request.ContentType =  "application/json" 
like image 133
nieve Avatar answered Sep 22 '22 06:09

nieve


For WebAPI >> If you are calling this POST method from fiddler, just add this below line in the header.

Content-Type: application/json 
like image 34
Rakesh Burbure Avatar answered Sep 19 '22 06:09

Rakesh Burbure


As answered by others the issue is with the ContentType. Should be 'application/json'.

Here is a sample with the old WebRequest

var parameters = Newtonsoft.Json.JsonConvert.SerializeObject(obj);

var req = WebRequest.Create(uri);

req.Method = "POST";
req.ContentType = "application/json";

byte[] bytes = Encoding.ASCII.GetBytes(parameters);

req.ContentLength = bytes.Length;

using (var os = req.GetRequestStream())
{
    os.Write(bytes, 0, bytes.Length);

    os.Close();
}

var stream = req.GetResponse().GetResponseStream();

if (stream != null)
    using (stream)
    using (var sr = new StreamReader(stream))
    {
        return sr.ReadToEnd().Trim();
    }
return "Response was null";
like image 30
martin Avatar answered Sep 21 '22 06:09

martin