How to use cURL in Unity

Hello, I was just wondering how I would use this cURL in unity:

curl https://api.ai21.com/studio/v1/j1-large/complete \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer YOUR_API_KEY' \
    -X POST \
    -d '{"prompt": "Life is like",
        "numResults": 1,
        "maxTokens": 8,
        "stopSequences": ["."],
        "topKReturn": 0,
        "temperature": 0.0
}'

I’m not really sure how to do it. I searched a bit on the internet and it seems that you need to use WWWform. Could someone help me and point me in the right direction? Thanks in advance

You would use UnityWebRequest.

Create it: Unity - Scripting API: Networking.UnityWebRequest.UnityWebRequest
Set the content type and data via Unity - Scripting API: Networking.UnityWebRequest.uploadHandler
Set the headers on it via:
Unity - Scripting API: Networking.UnityWebRequest.SetRequestHeader

2 Likes

Thanks for the response! Would it be possible to use httpRequest as well?

Not sure what you mean by that. UnityWebRequest is for making HTTP requests, same as cURL.

Sorry, for not responding in a bit. In the end I just used httpRequest and httpResponse from .Net. I just used the System.Net and System.IO namespaces.

Sorry to necro, but since i’m trying to figure out the same thing… How would I run a webrequest for this?

curl -s -X POST \
  -d '{"version": "1a4da7adf0bc84cd786c1df41c02db3097d899f5c159f5fd5814a11117bdf02b", "input": {"prompt": "..."}}' \
  -H "Authorization: Token $REPLICATE_API_TOKEN" \
  -H 'Content-Type: application/json' \
  "https://api.replicate.com/v1/predictions" | jq

I tried like this,

        var requestObj = new
        {
            version = "1a4da7adf0bc84cd786c1df41c02db3097d899f5c159f5fd5814a11117bdf02b",
            input = new
            {
                prompt = prompt
            }
        };

        string json = JsonUtility.ToJson(requestObj);
        UnityWebRequestrequest = UnityWebRequest.Post(replicateURL, json);

        request.downloadHandler = new DownloadHandlerBuffer();
        request.SetRequestHeader("Authorization", "Token " + apiToken);
        request.SetRequestHeader("Content-Type", "application/json");

I’ll admit, i don’t really understand posts, this script was written by ChatGPT lol, but I’ve tried making adjustments and changes and i keep getting a 400 bad request error. If it were my API key, wouldn’t it be a 401 error?

Then either throw it away and learn what you’re actually doing, or else ask ChatGPT.

Hint: nobody here knows what ChatGPT is doing either. We’re all working from the official Unity3D documentation and doing Software Engineering to arrive at an engineered solution to a problem.

Don’t think of it as an “all at once” problem. Get each piece working one step at a time.

Some more reading:

Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

https://discussions.unity.com/t/831681/2

https://discussions.unity.com/t/837672/2

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

https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity

1 Like

Well, this highly depends on what version of Unity you’re using. In the past (and the code looks like this) the Post method could only create post requests which sends URL encoded form data. So the post body you provide is expected to be url encoded form data. Of course you try to send just raw json text, so using this method would mangle the data. In the latest Unity versions they actually modified Post to have a 3rd parameter to specify the actual content type you want to send and also does not encode your body in a way that is unintuitive (as it was in previous Unity versions).

The “old” url-encoding behaviour has been moved into a separate method now: UnityWebRequest.PostWwwForm.

So if you’re stuck on an older Unity version, you can use the “Put” workaround which is usually the easiest solution. That means you just replace Post with Put and after the request is created, you change the request method back to “POST”. The Put method always had used raw up and download handlers. Another alternative is to manually create the UnityWebRequest through the constructor and then assign a up and downloadhandler yourself, encode your text as utf8 and set the request method yourself. You will find countless of questions in the forum, on Unity Answers and on StackOverflow about this. Since we don’t know what Unity version you have, you have to do some research on your own.

1 Like

I know how to do web requests, it’s just the weird variations of POST requests that throw me off, i figured i’d get some help from Chat GPT in figuring it out lol. But, i will say, my friend suggested i do this, and i regret trying Chat GPT to solve this, it’s useless lol, it even gave me some nonexistent websites a few times, and it often forgets namespaces

I’m using 2021.3.23, I thought i fixed the weird out of date bits Chat GPT gave me but maybe i missed some lol. OH! That worked, i changed the POST to a Put and assigned it like this

 UnityWebRequest request = UnityWebRequest.Put(API_ENDPOINT, requestData);
        request.method = "POST";

It SEEMS to be Posting properly, i’m getting a response and no 400 or 404 errors lol. Now i just need to hope i can download an AI generated 3D model with the JSON it returned. lol, thanks! I got it working thanks to you