Issues with multipart form data

Hi,
First of all, I must say I’m very impressed with the Scriptable app, and the possibilities it offers.

I’ve done a few simple scripts, which work very well, and now I’m trying to go to the next step with a script that involves calling a server API for image processing.

The API requires that the image will be submitted in a POST request, with the image as the only field in the form. The field name must be called “image”. The rest of the parameters are supplied as query string.
I tried to use both addFileToMultipart and addImageToMultipart and having issues with both of them.

For addFileToMultipart, everything seems to work well, except that the name of the parameter seems to be hard coded to “file”, ignoring the parameter I pass which is “image”.
Code sample:

    let req = new Request(getRecognizeUrl())
    req.method = "POST"
    req.addFileToMultipart(filePath, "image", fileName)
    let result = await req.loadJSON()

Charles output of start of body example:

    --Boundary-5C98CA10-E1B0-4D09-9C75-BFD42D8318F8
    Content-Disposition: form-data; name="file"; filename="A_20180810222450.JPG"
    Content-Type: application/octet-stream

For addImageToMultipart, I couldn’t even get anything in the form body, and there is no CONTENT-TYPE request header either.
The image I’m trying to post is valid, and I can see it via QuickLook.present.

Thanks for any help.
Yehuda

Hi Yehuda,

There’s a bug in the addFileToMultipart API where the supplied name is ignored. The issue will be fixed in the next build. I’m sorry for the inconvenience.

The addImageToMultipart suffers from the same issue but should otherwise work. Something like the following should work in your case.

let utis = ["public.image"]
let filePaths = await DocumentPicker.open(utis)
let filePath = filePaths[0]
let image = Image.fromFile(filePath) 
let req = new Request(getRecognizeUrl())
req.method = "POST"
req.addImageToMultipart(image, "image")
let result = await req.loadJSON()

Hi Simon,
Thanks for the quick reply.
I’ll check with the next version.
BTW, is there away to join the Beta program?

If you check the description post for this sub forum it has a link to sign up!

Oops, didn’t see the link to signup, since it was bellow all the tweets.
Anyway, I tried it now, and got this message:

The form Scriptable Beta Signup is no longer accepting responses.
Try contacting the owner of the form if you think this is a mistake.

Is there another link I should use?

All of the sudden I got A LOT of sign ups on that form. Spammers, I fear. So I decided to close the sign ups. I’m considering doing another round soon.

2 Likes

The thread has been updated to reflect that.

Thank you! (20 characters… :blush:)

1 Like

Hi Simon,
I checked the latest version (1.0.2).
I was able to post the image as a multipart form, but unfortunately the addImageToMultipart method still didn’t work well.
I used instead the new method: addFileDataToMultipart and that did the trick, although it requires an extra step of converting the Image type to Data via the Data.fromJPEG method.

Hi,

Can you elaborate on what issues you are seeing with addImageToMultipart? It is definitely supposed to work in the latest build.

I think it’s the same issue I had in version 1.0:
Body is empty, and request header is missing the form/multipart.

Here is the header being sent with addFileDataToMultipart:

POST /v2/recognize?recognize_vehicle=1&country=eu&secret_key=sk_SECRET HTTP/1.1
Host: api.openalpr.com
Content-Type: multipart/form-data; boundary=Boundary-4DB510E1-F7C0-49C4-9BD1-4AB8D1B90DDA
Connection: keep-alive
Accept: application/json
User-Agent: Scriptable/32 CFNetwork/975.0.3 Darwin/18.2.0
Content-Length: 1152166
Accept-Language: en-us
Accept-Encoding: br, gzip, deflate

And here is the header sent with addImageToMultipart:

POST /v2/recognize?recognize_vehicle=1&country=eu&secret_key=sk_SECRET HTTP/1.1
Host: api.openalpr.com
Connection: keep-alive
Accept: application/json
User-Agent: Scriptable/32 CFNetwork/975.0.3 Darwin/18.2.0
Accept-Language: en-us
Content-Length: 0
Accept-Encoding: br, gzip, deflate

@yehudab I’ve looked into this again and found the issue with addImageToMultipart. It’ll be fixed in the next beta bulid of 1.1.0. Sorry for the inconvenience.

1 Like

Thanks. I’m on the TestFlight Beta now, and have version 35. Is the fix included here, should I wait for build 36?

It’ll be included in build 36. I hope to send out the next build during the weekend.

1 Like

@simonbs,
Checked now with build 36, and it works well.
Thanks for the fix!

1 Like

Great! Thanks for getting back and for helping me debug this.

1 Like