#Web request not working on Unity side

1 messages · Page 1 of 1 (latest)

pure nacelle
#

Hey, I'm working on an project and integrating an Web api, This api is working fine in Any web api testing tools like postman, swagger, etc, but the same api doesn't work in unity enviroment neither in an android build (Api returns with error 403). I don't know what is happening, I'm attaching the response error's body, the code I'm using to integrate it. Please help

#

IEnumerator CallApi()
{
string jsonData = @"{
""deviceId"": ""aab1"",
""securityToken"": ""test"",
""sysConfigAsOfDate"": ""2025-08-18T05:45:36.769Z"",
""userConfigAsOfDate"": ""2025-08-18T05:45:36.769Z""
}";

    Debug.Log("Sending JSON: " + jsonData);

    byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);

    UnityWebRequest request = new UnityWebRequest("url", "POST");
    request.uploadHandler = new UploadHandlerRaw(bodyRaw);
    request.downloadHandler = new DownloadHandlerBuffer();

    // Match curl exactly
    request.SetRequestHeader("accept", "text/plain");
    request.SetRequestHeader("Content-Type", "application/json-patch+json");

    yield return request.SendWebRequest();

    if (request.result == UnityWebRequest.Result.Success)
    {
        Debug.Log("Response: " + request.downloadHandler.text);
    }
    else
    {
        Debug.LogError("Error: " + request.error);
        Debug.LogError("Response Code: " + request.responseCode);
        Debug.LogError("Response Body: " + request.downloadHandler.text);
    }
}
#

//private IEnumerator __LoginRoutine()
//{
// JSONObject data = new JSONObject();

//    data.Add("deviceId", "aa12");
//    data.Add("securityToken", "test");
//    data.Add("sysConfigAsOfDate", "2025-08-20T05:49:04.050Z");
//    data.Add("userConfigAsOfDate", "2025-08-20T05:49:04.050Z");

//    using (UnityWebRequest request = UnityWebRequest.PostWwwForm("url", data))
//    {
//        request.method = UnityWebRequest.kHttpVerbPOST;
//        yield return request.SendWebRequest();

//        //if there is an error in sending web request.
//        if (!string.IsNullOrEmpty(request.error))
//        {
//            Debug.Log(request.error);
//        }

//        //on success 
//        else
//        {
//            JSONObject response = JSON.Parse(request.downloadHandler.text) as JSONObject;
//            Debug.Log(response.ToString());

//        }

//        request.Dispose();

//    }

//}
#

These 2 returns the same error code 403

sly scaffold
#

"application/json-patch+json" is a bit strange content type for that data, are you sure that's what the other requests use? Does it work if you change it to "application/json"?

pure nacelle
#

I tried changing the content type to application/json, that too not working

#

Same response 403

sly scaffold
#

You'll have to show how the working request looks like. You can export the Postman request to curl and post that

pure nacelle
#

Okay send you the curl request

#

curl -X 'POST'
'https://interactivear.olefina.com/api/Start'
-H 'accept: text/plain'
-H 'Content-Type: application/json'
-d '{
"deviceId": "aa12",
"securityToken": "test",
"sysConfigAsOfDate": "2025-08-20T05:49:04.050Z",
"userConfigAsOfDate": "2025-08-20T05:49:04.050Z"
}'

sly scaffold
#

For some reason it doesn't like Unity's user agent. Try adding this: request.SetRequestHeader("User-Agent", "");

pure nacelle
#

I checked still 403

#

I tried fake values like samples from postman user agent and swager user agent exact values

#

S++ man, this worked, passing an empty string as user agent

#

Passing the postman or swagger didn't worked

sly scaffold
#

Yeah it's strange, they might have some kind of list of user agents they accept