#API Request Server Error

21 messages · Page 1 of 1 (latest)

clear berry
#

Context:
This is for the OpenAIKit project written in Swift, and I'm trying to implement the function to send in image edits; which requires to send the images as a Data object. So when making the field data to confirm to the file, I end up getting this error:

{
  "error": {
    "code": null,
    "message": "The server had an error processing your request. Sorry about that! You can retry your request, or contact us through our help center at help.openai.com if you keep seeing this error. (Please include the request ID 2ef432d2a77b5e94a2d6d61c15f1d0ed in your email.)",
    "param": null,
    "type": "server_error"
  }
}

Here is the function in question:

private func dataFormField(
            named name: String,
            data: Data,
            mimeType: String) -> Data
        {
            let fieldData = NSMutableData()

            fieldData.append("--\(boundary)\r\n")
            fieldData.append("Content-Disposition: form-data; name=\"\(name)\"; filename=\"\(name).png\"\r\n")
            fieldData.append("Content-Type: \(mimeType)\r\n")
            fieldData.append("\r\n")
            fieldData.append(data)
            fieldData.append("\r\n")

            return fieldData as Data
        }

Please let me know if you guys need any other information.

silver axle
#

Where do you put the prompt and the mask?

#

Or the image argument name

clear berry
# silver axle Or the image argument name

So this is in my playground project, though it will be the same once fully implemented.

This is the function that's submitting the form:

public func submitForm<T: Decodable>(
        _ type: T.Type = T.self,
        with url: URL,
        andApiKey apiKey: String? = nil,
        body: [String: Any]
    ) async throws -> T {
        if let apiKey = apiKey {
            let request = FormDataRequest(url: url)
            
            body.forEach { (key, value) in
                if let dataValue = value as? Data {
                    request.addDataField(named: key, data: dataValue, mimeType: "image/png")
                } else {
                    request.addTextField(named: key, value: "\(value)")
                }
            }
                
            var parsedRequest = request.asURLRequest()
            parsedRequest.allHTTPHeaderFields = ["Authorization" : "Bearer \(apiKey)"]
            
            let data = try await asyncData(with: parsedRequest)
            
            print(String(decoding: data, as: UTF8.self))
            
            return try await self.decodeData(with: data)
        }

        throw error.notImplemented
    }
#

And this is the struct for the parameter:

struct EditParams {
    let image: Data
    let mask: Data
    let prompt: String
    let numberOfImages: Int
    let size: String
    let responseFormat: String
    
    var body: [String: Any] {
        return ["image": image, "mask": mask, "prompt": prompt, "n": numberOfImages, "size": size, "response_format": responseFormat]
    }
}
#

And this is how the data is initialized:

guard let otterUrl = Bundle.main.url(forResource: "image", withExtension: "png") else { throw error.dataIsNil }
    guard let otter = try? Data(contentsOf: otterUrl) else { throw error.dataCannotBeConvertedOver }
    
    guard let maskUrl = Bundle.main.url(forResource: "mask", withExtension: "png") else { throw error.dataIsNil }
    guard let mask = try? Data(contentsOf: maskUrl) else { throw error.dataCannotBeConvertedOver }
#

Oh also this is the text form field function:

private func textFormField(named name: String, value: String) -> String {
            var fieldString = "--\(boundary)\r\n"
            fieldString += "Content-Disposition: form-data; name=\"\(name)\"\r\n"
            fieldString += "Content-Type: text/plain; charset=ISO-8859-1\r\n"
            fieldString += "Content-Transfer-Encoding: 8bit\r\n"
            fieldString += "\r\n"
            fieldString += "\(value)\r\n"

            return fieldString
        }
#

(Boundary is just a UUID string too)

silver axle
#

whats FormDataRequest

silver axle
#

ok I got this working

#

the way you're reading in images doesn't work

#

UIImage(named: "mask")?.pngData()

#

try that instead

#

though your error that you posted is a 500 and unrelated

#

try again?

#

also remember to include filename in the form data, not just content type

#

you dont need contenttype actually, just use filename

silver axle
#

actually images may be my issue since my image is almost too big (4MB)

clear berry
#

Out at the movies at the moment, will test this out tonight

clear berry
clear berry