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

后端 未结 3 781
借酒劲吻你
借酒劲吻你 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:44

    @Frode F. gave the right answer.

    By the Way Invoke-WebRequest also prints you the 200 OK and a lot of bla, bla, bla... which might be useful but I still prefer the Invoke-RestMethod which is lighter.

    Also, keep in mind that you need to use | ConvertTo-Json for the body only, not the header:

    $body = @{
     "UserSessionId"="12345678"
     "OptionalEmail"="MyEmail@gmail.com"
    } | ConvertTo-Json
    
    $header = @{
     "Accept"="application/json"
     "connectapitoken"="97fe6ab5b1a640909551e36a071ce9ed"
     "Content-Type"="application/json"
    } 
    
    Invoke-RestMethod -Uri "http://MyServer/WSVistaWebClient/RESTService.svc/member/search" -Method 'Post' -Body $body -Headers $header | ConvertTo-HTML
    

    and you can then append a | ConvertTo-HTML at the end of the request for better readability

提交回复
热议问题