Parse json issue using curly
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?
This means the server sent a response that is not a valid JSON, you have to double-check the response is valid.
@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.
Try setting your own parser to see if that helps, the default is simply calling JSON.parse in the output.
@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?
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
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.