- Home /
Question by
kaustubh_unity · Sep 07, 2020 at 03:05 PM ·
jsonwebrequestpost
Webrequest Post SetRequestHeader Problem
Hi, im trying to POST json data to server using webrequest. It works in postman where two headers are added i.e content-type and access-token. but it throws 403 error in unity. Following is the code im using. I don't encounter any issue with content type application/json, but the custom header SetRequestHeader is not working. Please help me with this issue , Thanks in advance.
private IEnumerator PostRequest(string url, string json, Action<string, bool> OnRequestProcessed)
{
var request = new UnityWebRequest(url, "POST");
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
request.uploadHandler.contentType = "application/json";
request.SetRequestHeader("access-token", DBManager.Instance.playerToken);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.LogError("web request error in Post method with responce code : " + request.responseCode);
OnRequestProcessed(request.error, false);
}
else
{
OnRequestProcessed(request.downloadHandler.text, true);
}
request.Dispose();
}
Comment