Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 289
U r s u s Avatar asked Jul 23 '14 15:07

U r s u s


People also ask

How do I create a curl request in PowerShell?

There are various ways to make a web request using the curl command: You can use “curl” or “Invoke-WebRequest” to get the same result. As mentioned above, “Invoke-WebRequest” is an Alias of “curl“. Similarly, you can use -ExpandProperty to get the detailed content of any information extracted using curl .

Can you use curl in PowerShell?

curl requires minimal user interaction and is therefore preferred for automation. curl in PowerShell uses Invoke-WebRequest . From PowerShell 3.0 and above, you can use Invoke-WebRequest , which is equivalent to curl .

Does invoke-WebRequest use curl?

By default all, the PowerShell cURL (Invoke-WebRequest) command allows all SSL/TLS protocols supported by the system.


2 Answers

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.

like image 80
mklement0 Avatar answered Oct 21 '22 23:10

mklement0


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
like image 31
Raf Avatar answered Oct 22 '22 00:10

Raf