Uploading an image with Request() but response returns {}

I’m creating a shortcut for my iPhone X, on iOS 15 developer beta 2.
All the shortcut does is run this script with the image uploaded from a share sheet, and then display a notification with the output of this script below.

const image = args.shortcutParameter

console.log("h") 

async function send() { 
  let req = new Request("myserver") 

  req.method = "POST" 
  req.headers = {"Content-Type":"multipart/form-data","key":"secretKey123"} 
  req.addImageToMultipart(FileManager.local().readImage(image.replace("file://", "")), "file") 

  return await req.loadJSON() 
}
Script.setShortcutOutput(send())

When running the shortcut, all I get from the notification is {}.
“h” doesn’t get logged either.
Also, when I write return "hi" instead of return await req.loadJSON(), the notification still returns {}.

How do I get the response back from the server?

The console is something that you are not accessing in the session, hence you won’t see your test output.

Look at the response property of the request object for the response as an alternative if you get nothing from the loadJSON. Maybe there could be a non JSON return?

For debugging, try your script outside of Shortcuts first using an alternate data source. Once you are happy with that, ry running the script in Scriptable from Shortcuts rather than inline in Shortcuts, and using alerts to check your script as it runs. Once you are happy with that, switch it up to run inline.

Hope that helps.

Ok, how do I get the path of a file, stored in, say, the Files app?

DocumentPicker is probably the easiest way for a test.

1 Like

Ah! Thank you very much!

Ok, so I cleaned up my code a bit, and turned it into this, but req.loadJSON() now doesn’t resolve. What do I do?

let image;
DocumentPicker.openFile().then(async imageRec => {
    image = imageRec
    console.log(image)
    async function send() {
        return new Promise((resolve, reject) => {
            let req = new Request("my server")
            req.method = "POST"
            req.headers = {
                "Content-Type": "multipart/form-data",
                "key":"key"
            }
            req.addImageToMultipart(FileManager.local().readImage(image.replace("file://", "")), "file")
            console.log("hi first")
            req.loadJSON().then(res1 => {
                console.log("hi")
                resolve(req.response)
            })
        })
    }
    console.log(await send())
})