Skip to content

Batching - unable to get .json() to work on json response #204

@nmetulev

Description

@nmetulev

Bug Report

Prerequisites

  • Can you reproduce the problem?
  • Are you running the latest version?
  • Are you reporting to the correct repository?
  • Did you perform a cursory search?

Description

I'm attempting to use the batching api to get a response for both the /me and the /me/photo/$value endpoint. However, using the .json() method on the me response throws an exception. In contrast, the .blob() on the photo response works. Alternatively, I've been able to get the json of me by going directly to the body of the response, which is no different that making the fetch response myself.

Console Errors: [Is there any console error]

Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1

Steps to Reproduce

    let photoRequest = new Request('/me/photo/$value', {
      method: 'GET'
    });

    let meRequest = new Request('/me', {
      method: 'GET'
    });

    let batchRequestContent = new BatchRequestContent([
      {
        request: photoRequest,
        id: '1'
      },
      {
        request: meRequest,
        id: '2'
      }
    ]);

    let content = await batchRequestContent.getContent();

    let response = await this.client
      .api('$batch')
      .post(content);

    let batchResponse = new BatchResponseContent(response);
    let responses = batchResponse.getResponses();
    
    // this works
    let blob = await responses.get('1').blob();

    // this throws exception
    // Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1
    let me = await responses.get('2').json();

    // alternative that works
    let me = response.responses[0].body

Expected behavior: [What you expected to happen]
I expect .json() to work or have documentation for the recommended way to access the response json

Actual behavior: [What actually happened]
.json() throws an exception

Additional context

I've found the batching docs are missing the final example of how to retrieve the content of each request.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions