Reputation: 1976
I am trying to access the stability.ai API from a Swift App. The documentation gives examples in Python, javascript and CURL but not Swift. They seem straightforward but I'm getting an error regarding the content-type as follows:
My code is:
var request = URLRequest(url: url)
request.setValue( authValue, forHTTPHeaderField: "Authorization")
let boundary = "------------------------some_boundary"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let accept = "image/*"
request.setValue(accept, forHTTPHeaderField: "Accept")
request.httpMethod = "POST"
request.httpBody = parametersJson
let task = URLSession.shared.dataTask(with: request) {
//perform fetch
}
The error I get is:
{\"success\":false,\"message\":\"Malformed FormData request. No initial boundary string (or you have a truncated message).\"}")
For reference, here is the API reference:
How to use
Please invoke this endpoint with a POST request.
The headers of the request must include an API key in the authorization field. The body of the request must be multipart/form-data. The accept header should be set to one of the following:
image/* to receive the image in the format specified by the output_format parameter.
application/json to receive the image encoded as base64 in a JSON response.
However, it also says:
content-type required string non-empty Example: multipart/form-data The content type of the request body. Do not manually specify this header; your HTTP client library will automatically include the appropriate boundary parameter.
And here is the sample code for cURL (that does not seem to mention multipart/form-data:
curl -f -sS "https://api.stability.ai/v2beta/stable-image/generate/sd3" \
-H "authorization: Bearer sk-MYAPIKEY" \
-H "accept: image/*" \
-F prompt="Lighthouse on a cliff overlooking the ocean" \
-F output_format="jpeg" \
-o "./lighthouse.jpeg"
I should note that if I do not send the content-type in the header, it gives error:
{\"name\":\"bad_request\",\"errors\":[\"content-type: must be multipart/form-data\"]}
What is the correct way to send this request to the Stability.api API?
Thanks in advance for any suggestions.
Upvotes: 0
Views: 185