Post request accessing the image.

hi everyone, i’m trying to use stable diffusion, i found a website i’m trying on it. in console it says succesful but how i can access to the image created by website?

  // Start is called before the first frame update
    void Start()
    {
        var body = @"{" + "\n" +
           @"    ""key"": ""api code here""," + "\n" +
           @"    ""prompt"": ""ultra realistic close up portrait ((beautiful pale cyberpunk female with heavy black eyeliner)), blue eyes, shaved side haircut, hyper detail, cinematic lighting, magic neon, dark red city, Canon EOS R3, nikon, f/1.4, ISO 200, 1/160s, 8K, RAW, unedited, symmetrical balance, in-frame, 8K""," + "\n" +
           @"    ""negative_prompt"": ""((out of frame)), ((extra fingers)), mutated hands, ((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), (((tiling))), ((naked)), ((tile)), ((fleshpile)), ((ugly)), (((abstract))), blurry, ((bad anatomy)), ((bad proportions)), ((extra limbs)), cloned face, (((skinny))), glitchy, ((extra breasts)), ((double torso)), ((extra arms)), ((extra hands)), ((mangled fingers)), ((missing breasts)), (missing lips), ((ugly face)), ((fat)), ((extra legs)), anime""," + "\n" +
           @"    ""width"": ""512""," + "\n" +
           @"    ""height"": ""512""," + "\n" +
           @"    ""samples"": ""1""," + "\n" +
           @"    ""num_inference_steps"": ""20""," + "\n" +
           @"    ""seed"": null," + "\n" +
           @"    ""guidance_scale"": 7.5," + "\n" +
           @"    ""webhook"": null," + "\n" +
           @"    ""track_id"": null" + "\n" +
           @"}";
        StartCoroutine(Post("https://stablediffusionapi.com/api/v3/text2img",body));

      
    }

    IEnumerator Post(string url, string bodyJsonString)
    {
        /*Form form = new WWWForm();
        form.AddField("input","body");
        using (UnityWebRequest www = UnityWebRequest.Post("https://stablediffusionapi.com/api/v3/text2img", form)) ;*/

           var request = new UnityWebRequest(url, "POST");
           byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
           request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
           request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
           request.SetRequestHeader("Content-Type", "application/json");
           yield return request.SendWebRequest();
           Debug.Log("Status Code: " + request.result);
    }

i tried to change this part, but it doesn’t work.

   Debug.Log("Status Code: " + request.result);

thanks everyone.

As I already told you yesterday, get it 100% working OUTSIDE of Unity first, then translate it to use UnityWebRequest:

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

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

Just a little bit of poking in the stablediffusion API documentation already shows that you’re not getting an image back but a json.
https://stablediffusionapi.com/docs/features/text-to-image
Scroll to the bottom and open up the Example Response.

You’d have to check the request.downloadHandler.text. Check whether this is the expected response. Then deserialize it into a class that represents the data structure.
It has a status which needs to be “success”.
It seems that the output array contains a url to the image(s).
Use UnityWebRequestTexture.GetTexture with that url. Once the request is complete use DownloadHandlerTexture.GetContent to get the image.

1 Like