#Help - C# DALL-E Image Variation API...

5 messages · Page 1 of 1 (latest)

glacial widget
#

What am I doing wrong here? I have no problem calling the DALL-E Image creation API, but I get the following error when calling the image variation API:

"'image' is a required property"

However, as you can see, I have the 'image' property defined. If I specify the Content-Type header as 'application/json' (which works fine for the image creation API), I get the following error instead:

"Invalid Content-Type header (application/json; charset=utf-8), expected multipart/form-data. (HINT: If you're using curl, you can pass -H 'Content-Type: multipart/form-data')"

Here is my code:

byte[] bits = null; //filled with actual image bits
var body = new
{
image = Convert.ToBase64String(bits),
n = 1,
size = "1024x1024"
};

var contentType = "multipart/form-data";
var uri = "https://api.openai.com/v1/images/variations";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer " + c_ApiKey);
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", contentType);

var content = new StringContent(
    Newtonsoft.Json.JsonConvert.SerializeObject(body), Encoding.UTF8, contentType);

var response = await client.PostAsync(uri, content);
var result = await response.Content.ReadAsStringAsync();

}

burnt crane
#

this error is when you arent sending the image in the proper format that multipart form data needs. is this javascript?

#

variations and edits needs multipart form data body because the image is attached as a binary file

#

oh its c#. follow something like https://www.c-sharpcorner.com/article/upload-any-file-using-http-post-multipart-form-data/ for how to send such a request

In this article, we will discuss how to upload any file using HTTP Post multipart/form-data in C#. I have tried my best to explain the use of the code described for multipart/form-data post needed many times while working with API’s in C#. I hope this would be helpful in the situations where we need to upload any file or image to the server us...

glacial widget