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
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.
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.
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).
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.
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
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