Hi @luee johnlu,
SharePoint Framework offers the SPHttpClient that you can use to connect to SharePoint REST APIs. A ready-to-use instance of the SPHttpClient is available on the web part/extension context and you can use it to do all kinds of web request. Following code snippet shows how you would use the SPHttpClient to retrieve the title of the current site:
this.context.spHttpClient
.get(`${this.context.pageContext.web.absoluteUrl}/_api/web?$select=Title`, SPHttpClient.configurations.v1)
.then((res: SPHttpClientResponse): Promise<{ Title: string; }> => {
return res.json();
})
.then((web: {Title: string}): void => {
console.log(web.Title);
});
The SPHttpClient offers basic functionality for performing the most common web requests. It allows you also, to configure your request by, for example, specifying request headers. For example, if you wanted to issue a web request without retrieving metadata, you'd use the following code:
this.context.spHttpClient
.get(`${this.context.pageContext.web.absoluteUrl}/_api/web?$select=Title`,
SPHttpClient.configurations.v1,
{
headers: [
['accept', 'application/json;odata.metadata=none']
]
})
.then((res: SPHttpClientResponse): Promise<{ Title: string; }> => {
return res.json();
})
.then((web: { Title: string }): void => {
console.log(web.Title);
});
You could refer to following document
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.