SPHttpClient is a subclass of HttpClient which inherits the functionalities from HttpClient, and is used to perform REST calls for SharePoint specific services. This will have default headers and digest information required for get or post calls.
For getting the information, get method is used. This indirectly calls the fetch method. The required parameters are,
- URL (string)
- Configurations (SPHttpClientConfiguration) - Used to set the default headers and version numbers. There is only one value available at this stage (v1).
- Options (ISPHttpClientOptions) - optional parameter, for setting the web URL explicitly
The get method is triggered using the context with the help of SPHttpClient. URL and configurations are passed as parameters. The URL can be constructed with the help of context available on the component.
The response will be in the type SPHttpClientResponse. JSON data will then be extracted from the response using the methods available.
The required packages to be imported on the webpart file.
import { SPHttpClient, SPHttpClientResponse, SPHttpClientConfiguration } from '@microsoft/sp-http';
- spHttpClient.get(`${this.context.pageContext.web.absoluteUrl}/_api/lists/GetByTitle('TestList')/items`,
- SPHttpClient.configurations.v1)
- .then((response: SPHttpClientResponse) => {
- response.json().then((responseJSON: any) => {
- console.log(responseJSON);
- });
- });
No comments:
Post a Comment