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.
Related
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.
I have an API Proxy in Apigee which is authenticated with an API key. I'm passing the key with my HTTP request header using cURL, with this command:
curl -v -H "apikey: my_key" http://api_org-test.apigee.net/v1/helloapikey
I get this error:
Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the
"apikey: my_key" value of type "System.String" to
type "System.Collections.IDictionary".
When I modify my policy to look for the key in query parameter rather than the header, it works fine. Am I missing something here?
Try this:
curl -v -H #{'apikey' = 'my_key'} http://api_org-test.apigee.net/v1/helloapikey
Note:
curl is an alias for the Invoke-WebRequest cmdlet:
Get-Alias curl
output:
CommandType Name
----------- ----
Alias curl -> Invoke-WebRequest
You could install curl:
https://stackoverflow.com/a/16216825/3013633
Remove existing curl alias by executing this command:
Remove-item alias:curl
Then your command will work:
curl -v -H "apikey: my_key" http://api_org-test.apigee.net/v1/helloapikey
None of the above answers worked for me (I got an error -- parse error near }).
This worked:
curl -X GET \
'http://api_org-test.apigee.net/v1/helloapikey' \
-H 'apikey: my_key'
PowerShell simply does not resolve the variable within your URL. You are trying to query the service at the URI http://$serverHost:1234/service which won't work. You could do
$serverHost = "myHost"
$service = "http://$serverHost`:1234/service"
Invoke-WebRequest $service -Method Get
Just to add this to the discussion, I had to both hash the api key, but leave the token call key phrase rather than change it to 'apikey'. That's just what worked for me!
curl -v -H #{'X-API-TOKEN' = '[*insert key here*]'} '*datacenter_url*)'
Also noteworthy to PowerShell newcomers, -v stands for verbose. This switch gives you a Cyan-colored text under the command in PowerShell ise about the command PS is running. Almost like a play-by-play commentary. Useful enough I thought I'd mention it.
I was pulling my hair out trying to get a command to work. I tried what I thought was a idnetical command, but with curl and it magically worked.
curl -X PUT -d param=value https://example.com/users/username?api_key=keyvalue&api_username=system
python httpie
http PUT https://example.com/users/username.json?api_key=value&api_username=system key=value
Both were validating and returning 200, but the http one did not follow through with changing the value like I was trying to do. Any idea why?
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
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!