UnityWebRequest and image upload

Hi,

I am trying to use uploadcare.com to manage and save pictures taking from within the app using a phones camera, but am struggling a bit to make create the upload form work.

Here is what I have so far:

// Encode texture into PNG
byte[] bytes = testImage.EncodeToPNG();
string encodedImage = Convert.ToBase64String(bytes);

// Create a Web Form
WWWForm form = new WWWForm();
form.AddField("UPLOADCARE_PUB_KEY", "xxxxxxxxxxxxxxxxxxxx");
form.AddField("UPLOADCARE_STORE", "1");
form.AddField("testfile.png", encodedImage);

using (UnityWebRequest www = UnityWebRequest.Post("https://upload.uploadcare.com/base/", form))
{
    yield return www.SendWebRequest();

    if (www.result != UnityWebRequest.Result.Success)
    {
        Debug.Log(www.error);
    }
    else
    {
        Debug.Log("Form upload complete!");
    }
}

The documentation from uploadcare looks like this:

{
  "UPLOADCARE_PUB_KEY": "YOUR_PUBLIC_KEY",
  "UPLOADCARE_STORE": "auto",
  "my_file.jpg": "@my_file.jpg"
}

It says in the documentation that the file should be: string

When running my UnityWebRequest I only get a 400 Bad Request back in the www.error?!?

Q: How can I get the full error response?
Q: Am i missing something in my understading of UnityWebRequest forms and the documentation?

Hoping for help and thanks in advance :slight_smile:

WWWForm is not JSON, it’s an old school HTML form. Your server requires JSON.

Here is the best way to reason about any networking issue:

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

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

APIs causing headaches… It’s like they’re playing a game of “who can make the developers suffer the most”!

Anyway, to answer your questions:

  • Have you tried using www.downloadHandler.text instead of www.error. It might give you a more detailed message about what went wrong.

  • As for missing something in your understanding of UnityWebRequest forms and the documentation - well, I’m not sure that’s even possible. The documentation is probably as clear as mud.