Formatting curl into a do shell script AppleScript

I’m trying to reformat a curl command into shell script as part of an AppleScript. I don’t have something right with the formatting. I get an error that says:

curl: (3) URL rejected: Malformed input to a URL function
curl: (3) bad range specification in URL position 2:
[{id: ImageID, scheme: image}, {id: ImageID, scheme: image}]
^" number 3

The curl code did work in Postman.
.
do shell script "curl --location 'https://example.canto.com/api/v1/batch/content'" & "--header 'Content-Type: application/json'" & "--header 'Authorization: Bearer MyToken'" & "--header 'Cookie: JSESSIONID= CookieID'" & "--data '[{'id': 'ImageID', 'scheme': 'image'}, {'id': 'ImageID', 'scheme': 'image'}]'"

1 Like

All space characters which separate the header and data parameters are missing, you can also insert the spaces inside the string literals but this makes it clearer, the ¬ is just a line separator.

do shell script "curl --location 'https://example.canto.com/api/v1/batch/content'" & space & ¬
	"--header 'Content-Type: application/json'" & space & ¬
	"--header 'Authorization: Bearer MyToken'" & space & ¬
	"--header 'Cookie: JSESSIONID= CookieID'" & space & ¬
	"--data '[{'id': 'ImageID', 'scheme': 'image'}, {'id': 'ImageID', 'scheme': 'image'}]'"

Adding the spaces got rid of the malformed URL error. Now I’m getting an error 3000 we ran into an unknown issue. This error only happens when I need to use the data parameter. I have another AppleScript that uses shell script/curl to search for and download images from Canto that works but it only has a URL and the authorization bearer token header. Is there anything else I might be doing wrong?

do shell script "curl --location 'https://example.canto.com/api/v1/related'" & ¬
" --header 'Content-Type: application/json'" & ¬
" --header 'Authorization: Bearer MyToken'" & ¬
" --header 'Cookie: JSESSIONID= CookieID'" & ¬
" --data '[{'relatedName': 'Related', 'relatedContents': [{'id': 'ImageID', 'scheme': 'image', 'primary': true}, {'id': 'ImageID', 'scheme':'image', 'primary': true}]}]'"

I figured it out. Apparently, single quotes don’t work within the JSON data parameter. So the single quotes just needed to be changed to a "

Between that and fixing the spaces, it’s now doing what it’s supposed to be doing within shell script /AppleScript :blush:

do shell script "curl --location 'https://example.canto.com/api/v1/related' --header 'Content-Type: application/json' --header 'Authorization: MyToken’ --header 'Cookie: JSESSIONID=CookieID’ --data '{\"relatedName\": \"Related\", \"relatedContents\": [{\"id\": \"ImageID\”, \"scheme\": \"image\", \"primary\": false}, {\"id\": \"ImageID\", \"scheme\": \"image\", \"primary\": true}]}'"

MacScripter edited it out but it should say the single quote needed to be changed to a backslash followed by a quote sign.

Put an extra backslash in front of the desired backslash, like so:

\\"

And you will get this:

\"

Please learn how to post code properly.
You put 3 ticks on a separate line (the tick is on the key with the tilde ~, next to the 1 key), then your code, then 3 more ticks on a separate line.

do shell script "curl --location 'https://example.canto.com/api/v1/related' --header 'Content-Type: application/json' --header 'Authorization: MyToken’ --header 'Cookie: JSESSIONID=CookieID’ --data '{\"relatedName\": \"Related\", \"relatedContents\": [{\"id\": \"ImageID\", \"scheme\": \"image\", \"primary\": false}, {\"id\": \"ImageID\", \"scheme\": \"image\", \"primary\": true}]}'"

You gonna have to escape that double quote otherwise it will close out your do shell script double quote.

There is the function “quoted form of”

1 Like