cURL to PowerShell - Double hash table in --data?

后端 未结 1 1839
南旧
南旧 2021-01-24 05:17

I\'ve been trying (and failing for hours) to convert this cURL script into PowerShell:

curl -X POST \\
  https://example.net \\
  -H \'Authorization: Bearer 1234         


        
1条回答
  •  囚心锁ツ
    2021-01-24 05:35

    • Your $Body variable contains a hashtable (@{ ... }), so you must explicitly convert it to JSON with ConvertTo-Json.

    • Additionally, because you're calling an external program, you must escape the " chars. in the JSON string as \"[1]:

    $CurlArgument = '-X', 'POST',
                    'https://example.net',
                    '-H', 'Authorization: Bearer 1234567890',
                    '-H', 'Content-Type: application/json',
                    '-d', 
                    (($Body | ConvertTo-Json) -replace '"', '\"')
    

    [1] This additional escaping requirement is highly unfortunate, but to date it has been kept around for the sake of backward compatibility. This GitHub docs issue tells the whole story.

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