Skip to content

Instantly share code, notes, and snippets.

@vman
Last active September 19, 2022 15:48
Show Gist options
  • Save vman/9f706f9779308f068120ffe212306456 to your computer and use it in GitHub Desktop.
Save vman/9f706f9779308f068120ffe212306456 to your computer and use it in GitHub Desktop.
Making a POST request to SharePoint from an SPFx webpart
private _makePOSTRequest(): void {
const spOpts: ISPHttpClientOptions = {
body: `{ Title: 'Developer Workbench', BaseTemplate: 100 }`
};
this.context.spHttpClient.post(`${this.context.pageContext.web.absoluteUrl}/_api/web/lists`, SPHttpClient.configurations.v1, spOpts)
.then((response: SPHttpClientResponse) => {
// Access properties of the response object.
console.log(`Status code: ${response.status}`);
console.log(`Status text: ${response.statusText}`);
//response.json() returns a promise so you get access to the json in the resolve callback.
response.json().then((responseJSON: JSON) => {
console.log(responseJSON);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment