1
  • Nock version: ^10.0.4

The request below always returns response as String. I need JSON format instead. Right now I cannot get res.data(return undefined), but I can see res as String.

  • How do I get JSON format of the response?

  • Here is my request:

    nock('https://api.smartsheet.com/2.0/sheets/')
         .defaultReplyHeaders({
             "accept-encoding":"gzip, deflate",
             "accept": "application/json",
             "content-type": "application/json"
          })
         .get('/')
         .reply(200, {"data" : "test"});
  • I tried use JSON.parse({"data": "test"), it still returns String as a response.
  • I also tried
    const buffer = Buffer.from(JSON.stringify({data: 'test'}));
    const compressed = zlib.gzipSync(buffer);
    nock('https://api.smartsheet.com/2.0/sheets/')
         .defaultReplyHeaders({
             "accept-encoding":"gzip, deflate",
             "accept": "application/json",
             "content-type": "application/json"
          })
         .get('/', compressed)
         .reply(200, () => compressed);

But I don't know how to send binary buffer in nock.

  • How do I send binary buffer in Get request?

  • Requesting code:

        this.smartsheet = SmartsheetClient({
            accessToken: this.config.get('smartsheet.oauth_token'),
        });
        const [getError, sheets] = await this.to(this.smartsheet.sheets.listSheets());
        if (getError) {
            throw new this.Doh('Fail to get sheets', getError);
        }
        return sheets.data;
user1874435
  • 161
  • 13
  • I am not familiar with the nock library, but I don't think you necessarily need to send a binary buffer, you might just need to add the JSON data header to the defaultReplyHeaders so that whatever is consuming the api knows to interpret the data as JSON. – stackuser83 Apr 19 '19 at 23:10
  • I have tried that, but it does not work. It always returns response as a whole string. I am really sure, the ```accept-encoding``` turns the response in different format. – user1874435 Apr 21 '19 at 04:00
  • please post an example of the code that calls the nocked endpoint. it could also be caused by configuration with the request library. – Catalyst Apr 21 '19 at 04:12
  • I have added the requesting code. In my case, try to access ```sheets.data``` is ```undefined```. – user1874435 Apr 22 '19 at 15:16
  • I have created a snippet here: https://github.com/nock/nock/issues/1510 – user1874435 Apr 22 '19 at 20:22

1 Answers1

0

The Smartsheet API always returns JSON by default. I'm not familiar with the nock library, but it seems like an issue with how this is handling the response. You might want to open an issue on their Github to ask about it if you don't hear on here. I did see in your example your URL is https://api.smartsheet.com/2.0/sheet/ The Smartsheet API uses plurals for the names in the requests, so it should be https://api.smartsheet.com/2.0/sheets/ instead for the List Sheets request.

daveskull81
  • 627
  • 4
  • 7