Using POST method in REST API in PowerShell

In one of previous blog post, we discussed how to work with using REST APIs in PowerShell. In this blog post, we’ll discuss how to use POST method in REST API.

POST involve sending both a body and headers.

  $person = @{
      first='joe'
      lastname='doe'
   }
   $body = (ConvertTo-Json $person)
   $hdrs = @{}
   $hdrs.Add("X-API-KEY","???")
   $hdrs.Add("X-SIGNATURE","234j123l4kl23j41l23k4j")
   $hdrs.Add("X-DATE","12/29/2016")
   Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType 'application/json' -Headers $hdrs
   ConvertTo-Json

The above on several lines is easier to read than one long line: Continue reading “Using POST method in REST API in PowerShell”

Working with ConvertTo-Json output issues

As you are aware, we can use ConvertTo-Json cmdlet to convert an object to Json output format using PowerShell. However, there is something you need to be aware of while using conversion. By default, it does not work with very large objects (containing of multiple sub-objects) and converts them properly. This is because of the fact that the Depth parameter for ConvertTo-Json has a default value of 2. Let’s understand what this means.

For our example, we’ll create a JSON file with below details first and save it on to your local machine as new-json.json

Continue reading “Working with ConvertTo-Json output issues”

Working with REST APIs in PowerShell

REST APIs are getting more and more common these days. It is important to learn them to be successful in the DevOps field. In previous blog post, we have used Invoke-WebRequest cmdlet to access the data available to an anonymous user.

PowerShell makes working with rest API’s easy. Starting with PowerShell v3, the cmdlets Invoke-RestMethod and Invoke-WebRequest were introduced. The difference between the two is quite small, Invoke-RestMethod simply being a slightly more convenient wrapper around Invoke-WebRequest as it only returns the content, omitting the headers. Continue reading “Working with REST APIs in PowerShell”