Image Variations in Swift, image sending issue

Hi there!
So I made this app:

And now wanted to expand it to use image variations as well.

However, when uploading the image data, I am unsure if I am approaching it correctly.
multipart/form data is in itself kinda over the top, but I managed to find this package that helps in wrapping everything up, but still not 100% if backend is expecting what I am sending :sweat_smile:

In he docs, it says STRING, so I have been trying to send:
image.pngData().base64EncodedData()

using this code below, with the help of the package:
https://github.com/FelixHerrmann/swift-multipart-formdata

static func variate(data: Data?, forUser user: String) async throws -> Response {
    guard let url = URL(string: "https://api.openai.com/v1/images/variations"),
          let promptData = data?.base64EncodedData() else { return Response(created: 0, data: []) }
    
    let boundary = try Boundary(uncheckedBoundary: "example-boundary")
    let multipartFormData = try MultipartFormData(boundary: boundary) {
      try Subpart {
        try ContentDisposition(uncheckedName: "image", uncheckedFilename: "image.png")
        ContentType(mediaType: .multipartFormData)
      } body: {
        promptData
      }
    }
    
    var request = URLRequest(url: url, multipartFormData: multipartFormData)
    request.httpMethod = "POST"
    request.addValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
    request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
    let (data, _) = try await URLSession.shared.data(for: request)
    return try decodeOrThrow(data: data)
  }

After this, I only get this error back

"message": "Uploaded image must be a PNG and less than 4 MB"
"invalid_request_error

Am I missing something obvious here? Not sure if the TYPE of the image I am sending is wrong or the approach in HOW it is being sent.

1 Like

Getting the same thing and I’ve also tried different combinations. The only time I had some success was with taking the base64 which was returned from generations endpoint and using that buffer to send to variations. If I create a buffer from an image and send, this isn’t working.

If you search the forums, some had problems with 24 vs 32 with alpha channel transparency…

Hope this helps!