Making a PowerShell POST request if a body param starts with '@'

后端 未结 3 770
借酒劲吻你
借酒劲吻你 2020-11-30 03:18

I want to make a POST request in PowerShell. Following is the body details in Postman.

{
  \"@type\":\"login\",
  \"username\":\"xxx@gmail.com\",
  \"passwor         


        
3条回答
  •  粉色の甜心
    2020-11-30 03:45

    You should be able to do the following:

    $params = @{"@type"="login";
     "username"="xxx@gmail.com";
     "password"="yyy";
    }
    
    Invoke-WebRequest -Uri http://foobar.com/endpoint -Method POST -Body $params
    

    This will send the post as the body. However - if you want to post this as a Json you might want to be explicit. To post this as a JSON you can specify the ContentType and convert the body to Json by using

    Invoke-WebRequest -Uri http://foobar.com/endpoint -Method POST -Body ($params|ConvertTo-Json) -ContentType "application/json"
    

    Extra: You can also use the Invoke-RestMethod for dealing with JSON and REST apis (which will save you some extra lines for de-serializing)

提交回复
热议问题