Applescript + REST? - rest

Does AppleScript have a way of interacting with REST APIs?
I realize I can
do shell script curl

I would use curl in
do shell script "#curl script here"
If you need help to get the correct curl statement I recommend postman, It helps me really to generate the right code. But remember If you have these: " to put an escape character in front: \".
So for example if i want to do a POST request to
https://api.widerstandsberechner.ch/api.php?firstcolor=red&secondcolor=orange&thirdcolor=yellow&fourthcolor=silver&hasFiveRings=0&resultInText=0
the AppleScript would look like this:
do shell script "curl -X POST -H \"Cache-Control: no-cache\" \"https://api.widerstandsberechner.ch/api.php?firstcolor=red&secondcolor=orange&thirdcolor=yellow&fourthcolor=silver&hasFiveRings=0&resultInText=0\""
And of course you can easy assign the result as a value:
set res to do shell script "curl -X POST -H \"Cache-Control: no-cache\" \"https://api.widerstandsberechner.ch/api.php?firstcolor=red&secondcolor=orange&thirdcolor=yellow&fourthcolor=silver&hasFiveRings=0&resultInText=0\""
Hope this helps!

Related

how submit curl request on windows powershell

I have a curl command that looks like the following
curl --cert "/C/tmp/keys/dev.crt" --key "/C/tmp/keys/dev.key" --cacert "/C/tmp/keys/root.crt" -X GET "https://urlofserver"
This works fine on a shell command. Now I need to submit the same using a powershell script. From what I researched I know you can use Invoke-WebRequest but not sure exactly how to pass these parameters. Would appreciate some help on this.

Putting curl into postman trouble

I was converting a cURL request from the command line command to the postman format and found something odd.
curl -u testclient:testpass http://localhost.com/ -d 'grant_type=client_credentials'
The above cURL command works in the terminal, but not in the Postman import section. I thought it wasn't a big deal, and that I would do it manually, but I can't seem to figure out what to do with the "-u testclient:testpass" portion of it.
-u testclient:testpass
Could someone please explain to me what this formatting means?
As read on the documentation,
-u, --user <user:password>
stands for user and password. So maybe you could select the Authorization tab, and select Basic Auth on the type. Then add the user and password.

Multiple query on gremlin-server REST

For example I want to query something like this,
>hercules= g.V().has('name','hercules')
>hercules.values()
>hercules.bothE()
How do I send those query to gremlin-server using REST ?
You can either separate each line with semicolons:
$ curl "http://localhost:8182?gremlin=x=100-1%3Bx-10"
{"requestId":"17bebb7e-3e99-4001-b33a-feca5b39b44f","status":{"message":"","code":200,"attributes":{}},"result":{"data":[89],"meta":{}}}
Note that with the curl statement above, the ; is urlencoded to "%3B" or you can just use newlines in a POST:
$ curl -X POST -d "{\"gremlin\":\"x=100-1\\nx - 10\"}" "http://localhost:8182"
{"requestId":"b5f28f38-e02f-4ab9-9888-3db389ff6f1c","status":{"message":"","code":200,"attributes":{}},"result":{"data":[89],"meta":{}}}

POST request with Powershell 2.0 using cURL

Scenario
Among other things, Powershell 2.0 doesn't have the useful cmdlet Invoke-RestMethod.
I can't upgrade to version 3 and most examples I've found use version 3.
I have found this article, which seems, however, too complicated for my simple scenario.
I need to write a Powershell script that POSTs data in Json format, e.g.
{"Id":5,"Email":"test#com","DataFields":null,"Status":0}
What I've tried
I am able to GET data. This is one of the scripts I have tried.
curl -v --user username:password https://api.dotmailer.com/v2/account-info
But, when I try to POST, I can't figure out where to put the body of the message in the script. This is what I've got so far:
curl -v -X POST -H "Accept: application/json" -H "Content-Type: application/json" -u username:password -d '{"Id":5,"Email":"test#com","OptInType":0,"EmailType":0, "DataFields":null,"Status":0}' https://api.dotmailer.com/v2/contacts
which returns the following error:
{"message":"Could not parse the body of the request based on the content type \"application/json\" ERROR_BODY_DOES_NOT_MATCH_CONTENT_TYPE"}*
Question
Can anyone advise on how to POST Json data from Powershell using cURL?
Any pointers to why I get the error I mentioned in the Waht I've tried section would be much appreciated.
Thanks.
Note that the question is about the curl.exe external program, not about PowerShell's Invoke-WebRequest cmdlet (which, unfortunately, is aliased to curl in later PowerShell versions, preempting calls to the external program unless the .exe extension is explicitly specified (curl.exe ...).
Unfortunately and unexpectedly, you have to \-escape embedded " instances in a string you pass as an argument to an external program.
Therefore, even though:
'{"Id":5,"Email":"test#com","DataFields":null,"Status":0}'
should work, it doesn't, due to a long-standing bug; instead, you must use:
'{\"Id\":5,\"Email\":\"test#com\",\"DataFields\":null,\"Status\":0}'
See this answer for more information.
From curl's man page it appears you need to use -d switch:
curl -v --user username:password -H "Content-Type: application/json" -d '{"Id":5,"Email":"test#com","DataFields":null,"Status":0}' https://api.dotmailer.com/v2/contacts

Is is possible to get around zsh autocorrect for one specific argument?

So, I''m testing out something and I have to run the same command repeatedly until it works. I'm doing something like this:
curl -X POST -d #filename.xml https://host.name
When I do that, zsh always replies with
zsh: correct '#filename.xml' to 'filename.xml' [nyae]? y
I want to get zsh to stop trying to autocorrect for only this command with only this argument. I eventually just made an alias in my .zshrc file and that solves the problem for me.
I'm just wondering if there is a better way to do this.
Prefix the word with \ to avoid spelling correction:
curl -X POST -d \#filename.xml https://host.name