REST API using PowerShell

REST API using PowerShell

REST API using PowerShell

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

You can interact with a REST API endpoint using PowerShell Invoke-WebRequest or Invoke-RestMethod modules. But for backward compatibility we will use Invoke-WebRequest to make different REST API requests like HEAD, GET, POST, PUT, PATCH, and DELETE.

If you wonder what REST API is, you can refer this blog post where I have given all the details to clear your doubts.

REST API using PowerShell

Prerequisite

Setup:

We will use a docker container that has REST API application pre-configured for this demo. Login to your shell and execute below command to launch a new docker container.

REST API HTTP HEAD request example using PowerShell Invoke-WebRequest module:

The HTTP HEAD method requests the headers that would be returned if the HEAD request’s URL was instead requested with the HTTP GET method. For example, if a URL might produce a large download, a HEAD request could read its Content-Length header to check the file size without actually downloading the file.

REST API HTTP GET request using PowerShell Invoke-WebRequest module:

GET method is used to retrieve resource information or to get information from a REST API endpoint. If the request is success, the endpoint response with a status code of 200 along with the requested data and if failed, response will contain the respective status codes like 404 or 400 etc.

Print REST API HTTP GET request and response details using PowerShell Invoke-WebRequest module

REST API HTTP POST request with data from a file using PowerShell Invoke-WebRequest module:

POST method is used to create a new resource. If the resource is created successfully the response will contain status code 201 and if failed, response will contain the respective status codes like 404 or 409 etc.

REST API HTTP POST request with data from script using PowerShell Invoke-WebRequest module:

How to use environment variable in data of PowerShell Invoke-WebRequest module?

So far, we have learned how to POST with data from file and from script using PowerShell Invoke-WebRequest module. But sometimes you may want to pass the data dynamically using environment variable or user defined variables in PowerShell Invoke-WebRequest cmdlet.

Next, we will discuss how to update an existing resource using PUT request and different between HTTP PUT and PATCH methods.

REST API HTTP PUT request using PowerShell Invoke-WebRequest module:

PUT method is used to update or replace an existing resource. If the resource is updated successfully the response will contains status code 200 and if failed, response will contain the respective status codes like 404 or 204 etc.

REST API HTTP PATCH request using PowerShell Invoke-WebRequest module:

PATCH is similar to PUT request but can update a subset of the resource without replacing the entire content. For example, if you want to update a specific field in your data then use PATCH request and if you want to update the entire content then use PUT request. If the resource is updated successfully the response will contains status code 200 and if failed, response will contain the respective status codes like 404 or 204 etc.

REST API HTTP DELETE request using PowerShell Invoke-WebRequest module:

How to authorize REST API requests in PowerShell Invoke-WebRequest module?

If your REST API endpoint has some authorization mechanism enabled (most have) then you need to authenticate your HTTP request with different authorization schema.

Basic Auth:

It is a simple authentication scheme built into the HTTP protocol. The client sends HTTP requests with the Authorization header that contains the word Basic, followed by a space and a base64-encoded(non-encrypted) string username: password. For example, to authorize as username / Pa$$w0rd the client would send.

Bearer Token:

Commonly known as token authentication. It is an HTTP authentication scheme that involves security tokens called bearer tokens. As the name depicts “Bearer Authentication” gives access to the bearer of this token.

API Key:

An API key is a token that a client provides when making API calls. With API key auth, you send a key-value pair to the API either in the request headers or query parameters. Some APIs use API keys for authorization.

Digest Auth:

Digest Authentication communicates credentials in an encrypted form by applying a hash algorithm to the username and the password, the password is converted to response and then it is sent to the server. After that server supplies nonce value, the HTTP method, and the requested URI.

OAuth:

OAuth permits client applications to access data provided by a third-party API. For example, as a user of a service you can grant another application access to your data with that service without exposing your login details.

Clean up:

Leave a Reply

Close Menu