Hello SharePoint developers,
In this post, I would like to share a useful technique that will allow you to test the SharePoint REST API with some great Lab tool such as POSTman.
In order to follow this technique, you will need the following tools installed on your computer:
- PnP PowerShell (september 2017 release at least)
Setup the app credentials and permissions
In order to setup the app credentials and permissions, you first need to go to the app registration page:
- Click the Generate button next to the Client Id field to generate a new one.
- Click the Generate button next to the Client Secret field to generate a new one.
- Set a title to any value
- Set the App domain to localhost
- Set the Redirect URL to https://localhost
- Click the Create button
( * bullets 4 & 5 values don’t really matter but the UI will not validate them if they have an incorrect format)
Keep safely the Client Id and Client Secret values because you will never be able to recover the secret if you lose it.
Then you need to set the permissions for this app
- 1) Copy/Paste the Client Id to the App Id field
- 2) Click the Lookup button
- 3) Copy/Paste the following XML in the Permission Request XML field
- 4) Click the Create button
Check the application requires the permission you expect it to and click the Trust it button.
Get the access token
In order to use Postman to call the REST API, you need to include the proper access token. With PnP PowerShell, you can get the app access token if you’re connected using app credentials
These commands above will connect you to your site with app credentials and copy the access token to the clipboard.
Issue requests with Postman
- Select the proper REST API URL
- Set the Authorization header with a value “Bearer #YOUR-TOKEN#“. Replace #YOUR-TOKEN# by the value you have in the clipboard.
- Optionally set a Accept header to “application/json” or “application/json; odata=verbose” to receive the data in the appropriate format.
- Click the Send button.
Here we are, we can issue HTTP calls to the SharePoint API with the appropriate credentials. With Postman, we can easily change the request headers and configuration to test different use cases.
Hope this will help!