Reputation: 251
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?
Upvotes: 25
Views: 118291
Reputation: 1045
For WebAPI >> If you are calling this POST method from fiddler
, just add this below line in the header.
Content-Type: application/json
Upvotes: 9
Reputation: 1
Serialize the data you want to pass and encode it. Also, mention req.ContentType = "application/json";
"martin " code works.
LoginInfo obj = new LoginInfo();
obj.username = uname;
obj.password = pwd;
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();
}
Upvotes: 0
Reputation: 376
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";
Upvotes: 3
Reputation: 320
this is sample of a code i created for web api function that accepts json data
string Serialized = JsonConvert.SerializeObject(jsonData);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpContent content = new StringContent(Serialized, Encoding.Unicode, "application/json");
var response = await client.PostAsync("http://localhost:1234", content);
}
Upvotes: 0
Reputation: 21
I renamed my project and updated all of the namespaces to correlate after which I got this exact same message. I realized that I had not updated the namespaces in the web.config (name and contract):
<system.serviceModel>
<services>
<service name="X.Y.Z.Authentication" behaviorConfiguration="ServiceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="X.Y.Z.IAuthentication" behaviorConfiguration="web" bindingConfiguration="defaultRestJsonp"></endpoint>
</service>
</...>
</..>
Hope this helps anyone reading this.
Upvotes: 1
Reputation: 69
Im not 100% sure but I guess you have to send the text as a byteArray, try this:
req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentType = "text/json";
byte[] byteArray2 = Encoding.ASCII.GetBytes(body);
req.ContentLength = byteArray2.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(byteArray2, 0, byteArray2.Length);
newStream.Close();
Upvotes: 0