POST request with Powershell 2.0 using cURL

前端 未结 2 1270
小蘑菇
小蘑菇 2021-01-12 17:28

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

相关标签:
2条回答
  • 2021-01-12 18:03

    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
    
    0 讨论(0)
  • 2021-01-12 18:14

    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.

    0 讨论(0)
提交回复
热议问题