- Home /
REST API Response Error: 406 - not acceptable
enter code here
Hi , I am using PHP on server side & the api that I want to use is successfully tested on POSTMAN. The api works in both form-data & raw under body.
I am unable to get response for the simple POST method API of php from UNITY. I have mess up with : unityWebRequest, WWForm. WWW .
Kindly please review my sample code .
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
public class Postwa : MonoBehaviour
{
void Start()
{
StartCoroutine(Upload());
}
IEnumerator Upload()
{
WWWForm form = new WWWForm();
form.AddField("licenseKey", "12345678");
Debug.Log(form.data.ToString());
using (UnityWebRequest www = UnityWebRequest.Post("https://mindplugged.com/space/methodName", form))
{
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else
{
Debug.Log(www.responseCode.ToString());
Debug.Log(www.downloadHandler.text);
}
}
}
}
Thanks in Advance
Answer by Aelcyx · Oct 05, 2017 at 09:24 PM
I found that adding these headers made it work:
headers.Add("Accept", "*/*");
headers.Add("Accept-Encoding", "gzip, deflate");
headers.Add("User-Agent", "runscope/0.1");
I figured this out by going to hurl.it, trying the request there and then looking at the response data via browser tools.
It works for POST but for GET and Asset bundles you need to upgrade to UnityWebRequest like this:
var www = UnityWebRequest.GetAssetBundle("https://...");
www.SetRequestHeader("Accept", "*/*");
www.SetRequestHeader("Accept-Encoding", "gzip, deflate");
www.SetRequestHeader("User-Agent", "runscope/0.1");
yield return www.SendWebRequest();
Thank you for this. Was on this for hours. The issue was the User-Agent from my end. Server was saying $$anonymous$$od Security was blocking the calls.