Unable to send PUT request via UnityWebRequest

I’m pulling my hair out with this one…

I need to send a PUT request to my website to allow a license to be changed from Delivered to Active, The API documentation for the license key provider I’m using says you need to send a body text in the PUT request.

My code looks like this

IEnumerator PutUpdateLicense()
{
    if (activationCheck == true)
    {
        string authorisation = authenticate("****", "****");
        string info = "{'\','status':'ACTIVE','\'}";
        using UnityWebRequest licenseRequest = UnityWebRequest.Put(URL + "licenses/" + inputLicense, info);
        {
            Debug.Log(UnityWebRequest.Put(URL + "licenses/" + inputLicense, info));
            licenseRequest.SetRequestHeader("AUTHORIZATION", authorisation);
            byte[] myData = System.Text.Encoding.UTF8.GetBytes(info);
            UploadHandlerRaw uH = new UploadHandlerRaw(myData);
            licenseRequest.uploadHandler = uH;
            licenseRequest.SetRequestHeader("Content-Type", "application/json");


            yield return licenseRequest.SendWebRequest();

            if (licenseRequest.result == UnityWebRequest.Result.ConnectionError)
            {
                Debug.LogError(licenseRequest.error);
            }
            else
            {
                updatedCheck = true;
                ValidateKeyButton();
            }
        }
    }
}

If I send an activation call (Which is run before the above code), it activates the license fine as this arrives in Debug.Log

{“success”:true,“data”:{“id”:11,“orderId”:78,“productId”:58,“userId”:1,“licenseKey”:“PT-3415-3822”,“expiresAt”:“2024-03-21 13:01:50”,“validFor”:3,“source”:1,“status”:2,“timesActivated”:1,“timesActivatedMax”:1,“createdAt”:“2024-03-18 13:01:50”,“createdBy”:1,“updatedAt”:“2024-03-18 13:03:17”,“updatedBy”:1,“activationData”}

Then the PutUpdateLicense code runs and connects as it doesn’t give back an error loop, It should then change the status from “2” to “3” to show it’s active.

Then once the UpdateChecked is set to true, it then validates the license to make sure the changes have been made, although it comes back with a successful connection the status remains at “2” (marked in bold)

{“success”:true,“data”:{“id”:11,“orderId”:78,“productId”:58,“userId”:1,“licenseKey”:“PT-3415-3822”,“expiresAt”:“2024-03-21 13:01:50”,“validFor”:3,“source”:1,“status”:2,“timesActivated”:1,“timesActivatedMax”:1,“createdAt”:“2024-03-18 13:01:50”,“createdBy”:1,“updatedAt”:“2024-03-18 13:03:17”,“updatedBy”:1,“activationData”}

I’ve tried looking for other guides but most of them deal with POST or GET, I have a feeling that the json string might be wrong because if I use ReqBin and set it up for the PUT request, it goes through absolutely fine (See uploaded image)

Here’s how to debug networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

And setting up a proxy can be very helpful too, in order to compare traffic:

1 Like

I’ve already done the debugging with ReqBin (Which is an online API checker) and the first link you put up shows that user being stuck at the same point I am (and the responses stopped right there).

I can connect to the database fine as the PutUpdateChecker doesn’t throw an error, I think the issue might be with the json data being sent…i’m not sure if the body is correctly formatted or even if UnityWebRequester is correctly sending it.

Because in ReqBin I can set the body as such

{
“status” : “ACTIVE”
}

but I have it down as

string info = “{‘',‘status’:‘ACTIVE’,’'}”;

Yes because what you send is not even valid json. Pure json does only accept double quotes for key and value encapsulation. Apart from that the whole structure doesn’t make sense, you have two commas which means you have 3 values. However the first and last would be just two string values and the middle one is a key-value-pair. This just makes no sense. Maybe you wanted to excape the double quotes? If so, you have to do:

string info = "{\"status\":\"ACTIVE\"}";

This would give you the json string:

{"status":"ACTIVE"}

edit

ps: With my SimpleJSON parser you could build that json simply like this:

JSONNode n = new JSONObject();
n["status"] = "ACTIVE";
string info = n.ToString();

Though any other json framwork would work. Though object mappers would require you to create a serializable class that contains a string field called “status”.

1 Like

THANK YOU!

I just made that small change, tried it and it worked straight away :slight_smile:

Now I can get back to what I was supposed to be doing several hours ago.

Honestly, you have no idea how happy I am now!

2 Likes

Nice spot Bunny…

For OP, don’t forget these links… very handy whenever you wrassle with JSON:

https://csharptojson.com

1 Like

Could have done with these earlier :smile:

Ah well, it works now, but if this comes up again, it might be worth pointing people to this thread.

This helped a lot as well, especially with the PUT requesting

https://forum.unity.com/threads/posting-raw-json-into-unitywebrequest.397871

That helped me get most of the code correct, it was just the JSON string that was causing the issue.

1 Like