Quantcast
Channel: Questions in topic: "webrequest"
Viewing all articles
Browse latest Browse all 387

Webrequest Post SetRequestHeader Problem

$
0
0
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 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(); }

Viewing all articles
Browse latest Browse all 387

Trending Articles