How to add custom header and body to SPHttpClient.post method
Category
- [x] Question
- [ ] Typo
- [x] Bug
- [ ] Enhancement / Suggestion
- [ ] Additional article idea
Expected or Desired Behavior
To be able to add my custom header/body in order not to attach the body of post request into REST URI
Observed Behavior
I declare a var as ISPHttpClientOptions and pass it to the post method
const opt:ISPHttpClientOptions = {
header: {'Content-Type': 'some value'},
body: {my bodyJson}
}
this.spHttpClient.post(url, SPHttpClientConfigurations.v1, opt)
and get this error
Type {headers:{...}, body: {...}} is not assignable to type ISPHttpClientOptions As i read the documentation of ISPHttpClientOptions it only has webUrl field, so how am i supposed to pass custom data to post request?
Thank you
#spfx
This seems to be working just fine:
import { SPHttpClient, ISPHttpClientOptions } from '@microsoft/sp-http';
const opt: ISPHttpClientOptions = { headers: { 'Content-Type': 'some value' }, body: { my: "bodyJson" } };
this.context.spHttpClient.post('', SPHttpClient.configurations.v1, opt);
@artxach - Two things from @waldekmastykarz 's post. 1 - he is quoting bodyJson, and 2, you appear to be accessing sphttpClient off of the webpart itself. Sounds like you are running an old version of the framework. You should upgrade to 1.0.
thanks @waldekmastykarz and @patmill.
I re-checked and seems errors were caused by some quotes and {}
Not working
const opt:ISPHttpClientOptions={headers: { 'Content-Type': 'value' }, body: { my: "bodyJson" } };
Working
const opt:ISPHttpClientOptions={headers: { 'Content-Type': 'value' }, body:"value" };
what about spHttpClient - i do use GA version of SPFx, and use webPart.context.spHttpClient - is it wrong?
@waldekmastykarz @patmill
I had an issue with this.
const opt: ISPHttpClientOptions = { headers: { 'Authorization': 'Bearer ' + accessToken } };
httpClient.get('https://test./api/',
HttpClient.configurations.v1,
opt)
I checked request header but the there is no Authorization header. Also tried with XMLHttpRequest and get the same error. Not sure why Authorization gone
When I send XMLHttpRequest (which include copy token) from console in separate window ==> It works
hi @honb , seems you are creating ISPHttpClientOptions variable then use it in simple httpClient instead of SPHttpClient. Try to use the spHttpClient property of the web part context.
Thanks @artxach for your answer. I also tried with spHttpClient & ISPHttpClientOptions but I get the same result. Please refer to #https://github.com/SharePoint/sp-dev-fx-webparts/issues/240 for more details.
ISPHttpClientOptions extends IHttpClientOptions which extends RequestInit. It is in RequestInit that the headers property is found. The linter considers { header: { 'Authorization': 'Bearer ' + accessToken } } an error because the accepted type for the headers property is 'Headers | string[][]'. Thus, for the linter to accept the assigned value, you must do one of the following:
this.context.ISHttpClient.get('[your URL]', HttpClient.configurations.v1, { headers: [[ 'Authorization', 'Bearer ' + accessToken]]);let h = new Headers(); h.append('Authorization', 'Bearer ' + accessToken); this.context.SPHttpClient.get('[your URL]', HttpClient.configurations.v1, h);
Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues