- Home /
Retrieving json with WWW raises 415 Unsupported Media Type
Hey Guys,
I've been using Uniweb for my web requests, but I had problem with iOS8 so I'm changing it back to plain WWW. I used it like so and it has worked when my content type is "application/x-www-form-urlencoded".
I used it like so :
string myUrl ="someUrl.com/someFolder"
WWWForm form = new WWWForm();
form.AddField("Content-Type", "application/x-www-form-urlencoded");
form.AddField("Content-length", "0");
var headers = new Hashtable();
headers.Add("WWW-Authenticate","xxxxx");
WWW request = new WWW(url,form.data,headers);
It works fine but when I use it for content "application/json", I get the error 415 Unsupported Media Type. I read that I missed the Header "Accept", "application/json" so I tried to add it to the form and it didn't work than I added it to the headers and it didn't worked either.
Does anyone have an idea ?
Thanks a lot.
What is the server platform that you're using as target for your requests? Is it IIs?
@Siflou Did you find a solution for this? I'm having the same issue.
Do you already have tried to send your request with other tool, like that bellow? https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo Always I get an error with Api requests in Unity, I like to test in this client tool in order to see if the error is in the unity code or in the data I am submiting.
Yeah, an outside tool is actually the best way to tell if things are going wrong in Unity or in the actual web request itself. In my case, using the tool, I realized I was sending the wrong header. I'd recommend everyone uses a third party tool to confirm the validity of their requests. Postman is the tool I use, but anyone will do.
Answer by Giver · Jan 05, 2016 at 03:22 PM
I have solved this problem. You can try like this. //First , you need to convert your post data into byte[].
WWWForm form=new WWWForm(); string body=LitJson.JsonMapper.ToJson( /here is you data/ ); byte[] data=System.Text.Encoding.UTF8.GetBytes(body);
//then set the headers Dictionary headers=form.headers; headers["Content-Type"]="application/json";
WWW www=new WWW(url,data,headers);
You can try this.I have successed~ I feel good . hahaha
I couldn't have this work until I defined headers variable as follows: Dictionary<string, string> headers = form.headers;
He didn't mark his code as code so the angle brackets <> got stripped away as they are recognised as HT$$anonymous$$L tags.
Your answer
Follow this Question
Related Questions
Max String Length in WWW url's? 1 Answer
WWW.EscapeURL Not Work Well? 2 Answers
How to assign texture from url 1 Answer
Using WWW to simply post a slab of text .. ala JSON 2 Answers
Loaclization in WebGL? 1 Answer