OPEN TO WORK

Available for new opportunities! Let's build something amazing together.

Perform other types of HTTP requests with HttpClient and SPHttpClient in SPFx projects

SharePoint Framework SPFx
post

This post is over a year old, some of this information may be out of date.

If you already used the HttpClient and SPHttpClient in your SharePoint Framework projects, you might have noticed that you have two types of request methods options: GET and POST. These two types of requests are the most used ones, but what if you want to use other HTTP methods like PUT, PATCH, DELETE?

If you need to make calls with other HTTP methods, you can make use of the fetch which is available on both the HttpClient and SPHttpClient classes. The fetch method allows you to specify the HTTP method type per request yourself. Fetch is also used behind the scenes when you are using the GET and POST methods.

Here is an example of how you can make use of the fetch method and specifying the request method to perform:

this.props.context.spHttpClient.fetch(restUrl, SPHttpClient.configurations.v1, {
method: "PATCH",
body: JSON.stringify({
"Something": "Some content to patch"
})
}).then((response: SPHttpClientResponse) => {
// Do your thing
});

Here is an example with the HttpClient, but you will notice that is not that different to use:

this.props.context.httpClient.fetch(restUrl, HttpClient.configurations.v1, {
method: "DELETE"
}).then((response: HttpClientResponse) => {
// Do your thing
});

Related articles

Report issues or make changes on GitHub

Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.

Comments

Elio Struyf

Solutions Architect & Developer Expert

Loading...

Let's build together

Manage content in VS Code

Present from VS Code