sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

How to add custom header and body to SPHttpClient.post method

Open artxach opened this issue 9 years ago • 8 comments

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

artxach avatar Mar 20 '17 16:03 artxach

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);

waldekmastykarz avatar Mar 20 '17 19:03 waldekmastykarz

@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.

patmill avatar Mar 20 '17 20:03 patmill

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?

artxach avatar Mar 22 '17 12:03 artxach

@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

honb avatar Jun 13 '17 08:06 honb

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.

artxach avatar Jun 14 '17 09:06 artxach

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.

honb avatar Jun 14 '17 10:06 honb

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);

paulewert avatar Dec 07 '17 21:12 paulewert

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

msft-github-bot avatar Jan 29 '20 18:01 msft-github-bot