Sp-dev-docs: SPHttpClientBatch POST-calls not working

Created on 23 May 2017  路  6Comments  路  Source: SharePoint/sp-dev-docs

I'm new to this. Sorry if I do this wrong.
But I believe something have happen to SPHttpClientBatch. My batched REST API POST calls have stopped working. The GET calls in the batch still works and the POST calls works then used separately on SPHttpClient.

Category

  • [ ] Question
  • [ ] Typo
  • [x] Bug
  • [ ] Additional article idea

Expected or Desired Behavior

HTTP/1.1 200 OK

Observed Behavior

HTTP/1.1 400 Bad Request
CONTENT-TYPE: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":"Invalid JSON. More than one value was found at the root of the JSON content. JSON content can only have one value at the root level, which is an array, an object or a primitive value."}}
--batchresponse_a67ce37e-3c36-44c1-ad03-bb8e82db89ee--

Steps to Reproduce

https://github.com/vman seems to experience the same issue. His repo: https://github.com/vman/SPFx-REST-Operations uses the same batched REST API calls. It also worked fine earlier.

private _makeSPHttpClientBatchRequest(): void {
    const spHttpClient: SPHttpClient = this.context.spHttpClient;
    const currentWebUrl: string = this.context.pageContext.web.absoluteUrl;

    const spBatchCreationOpts: ISPHttpClientBatchCreationOptions = { webUrl: currentWebUrl };
    const spBatch: SPHttpClientBatch = spHttpClient.beginBatch(spBatchCreationOpts);

    const getMyProperties: Promise<SPHttpClientResponse> = spBatch.get(`${currentWebUrl}/_api/SP.UserProfiles.PeopleManager/GetMyProperties`, SPHttpClientBatch.configurations.v1);
    const getWebTitle: Promise<SPHttpClientResponse> = spBatch.get(`${currentWebUrl}/_api/web/title`, SPHttpClientBatch.configurations.v1);

    const currentTime: string = new Date().toString();

    const batchOps: ISPHttpClientBatchOptions = {
      body: `{ Title: 'List created with SPFx batching at ${currentTime}', BaseTemplate: 100 }`
    }; 

    const createList: Promise<SPHttpClientResponse> = spBatch.post(`${currentWebUrl}/_api/web/lists`, SPHttpClientBatch.configurations.v1, batchOps);

    spBatch.execute().then(() => {
      getMyProperties.then((response: SPHttpClientResponse) => {
        response.json().then((props: any) => {
          console.log(props);
        });
      });

      getWebTitle.then((response: SPHttpClientResponse) => {
        response.json().then((webTitle: string) => {
          console.log(webTitle);
        });
      });

      createList.then((response: SPHttpClientResponse) => {
        response.json().then((responseJSON: any) => {
          console.log(responseJSON);
        });
      });
    });
  }

Most helpful comment

Since sp-http is packed into our bootloader, and since your problem only repros when talking to a real SharePoint server, you won't need to upgrade your NPM packages. You should see this fix as soon as we release the updated bundle to our CDN servers. It should be sometime in the next week. (If not, please reopen this issue!)

All 6 comments

Thanks for the feedback. We will investigate the issue.

Could you try this experiment:

  1. Install the Fiddler tool from http://www.telerik.com/fiddler

  2. Capture a Fiddler trace of your failing batch request

  3. Click on the request in the Fiddler window, and press "E" for the "Replay and Edit" command

  4. Go to the Request pane -> Inspectors -> Raw, and add an extra newline to the very end of the request text (after the "--batch_7e63aefc-758f-4ab3-92eb-29c218d3a238--")

  5. Press the green button "Run to completion" to reissue your request with this change.

Let us know if the request is successful. (If so, your problem is a known issue that will be fixed in the next release.)

Hi @pgonzal, Yes I can confirm that the fiddler trick works. Thanks!

When you say the next release, you mean the release after the 1.1.1 version of the generator right? Because I am getting the same issue in the 1.1.1 version.

Since sp-http is packed into our bootloader, and since your problem only repros when talking to a real SharePoint server, you won't need to upgrade your NPM packages. You should see this fix as soon as we release the updated bundle to our CDN servers. It should be sometime in the next week. (If not, please reopen this issue!)

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

patrick-rodgers picture patrick-rodgers  路  3Comments

StfBauer picture StfBauer  路  3Comments

StfBauer picture StfBauer  路  3Comments

zerovectorspace picture zerovectorspace  路  3Comments

mikeparkie picture mikeparkie  路  3Comments