how to recreate a working CURL command with Invoke-WebRequest in Powershell

后端 未结 3 509
生来不讨喜
生来不讨喜 2020-12-28 20:27

This curl command works as desired:

curl -H \"X-Api-Key:j65k423lj4k2l3fds\" `
     -X PUT `
     -d \"alerts_enabled=true\" `
        https://some/working/fi         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-28 21:10

    Try adding the parameter -ContentType e.g.:

    Invoke-WebRequest -Headers @{"X-Api-Key" = "j65k423lj4k2l3fds"} -Method PUT `
                      -Body "alerts_enabled=true" -Uri https://some/working/file.xml `
                      -ContentType application/x-www-form-urlencoded
    

    That results in a request that looks like this (from Fiddler):

    PUT http://some/working/file.xml HTTP/1.1
    X-Api-Key: j65k423lj4k2l3fds
    User-Agent: Mozilla/5.0 (Windows NT; Windows NT 6.2; en-US) WindowsPowerShell/5.0.9701.0
    Content-Type: application/x-www-form-urlencoded
    Host: some
    Content-Length: 19
    Expect: 100-continue
    
    alerts_enabled=true
    

    For testing, I changed the URL from https to http. If that doesn't work, go download Fiddler and inspect the RAW request when curl is used to see what is different.

提交回复
热议问题