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()
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.
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()
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.
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.
@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.