node-libcurl icon indicating copy to clipboard operation
node-libcurl copied to clipboard

Parse json issue using curly

Open domadev812 opened this issue 4 years ago • 5 comments

Hi,

I am trying to call api using curly like this.

const response = await curly.get(IPONLY_API_URL, {
      httpHeader: [
        'Content-Type: application/json',
        `Authorization: Basic ${base64.encode(username + ":" + password)}`
      ],
      caInfo: certFilePath,
    })

And I get the issue something like this

Error: curly failed to parse "application/json" content as JSON. This is generally caused by receiving malformed JSON data from the server.
    You can disable this automatic behavior by setting the option curlyResponseBodyParser to false, then a Buffer will be returned as the data.
    You can also overwrite the "application/json" parser with your own by changing one of the following:
      - curly.defaultResponseBodyParsers['application/json']
      or
      - options.curlyResponseBodyParsers = { 'application/json': parser }
    
    If you want just a single function to handle all content-types, you can use the option "curlyResponseBodyParser".

Could you tell me what is the solution?

domadev812 avatar Aug 26 '21 02:08 domadev812

This means the server sent a response that is not a valid JSON, you have to double-check the response is valid.

JCMais avatar Oct 10 '21 18:10 JCMais

@JCMais I checked the response and it has correct JSON format. I guess the content is too long, because if I download the content to the file, it is 600MB.

domadev812 avatar Oct 10 '21 18:10 domadev812

Try setting your own parser to see if that helps, the default is simply calling JSON.parse in the output.

JCMais avatar Oct 10 '21 18:10 JCMais

@JCMais I tried to set own parser, but I can't find the solution how can I create the own parse. Could you guide me?

domadev812 avatar Dec 29 '21 14:12 domadev812

There is not much documentation for that yet, but here are some details: https://github.com/JCMais/node-libcurl/blob/0dcbb5df9d7b27dc694851b2ea164495792f8dc2/CHANGELOG.md#breaking-change-1

JCMais avatar Jan 04 '22 16:01 JCMais

To keep the maintainability of this project, I am closing old tickets (sorry if your ticket was kept open for so long without feedback).

If the issue continues with the latest version of node-libcurl, please feel free to create a new ticket.

JCMais avatar Feb 13 '24 21:02 JCMais