Below is the code written in my get method. I don't know why am I facing this problem. How to solve it, please provide some code.
app.get("/",function(req,res)
{
const url = "https://jsonplaceholder.typicode.com/users"
https.get(url,function(response)
{
console.log(response.statusCode);
response.on("data",function(data)
{
const apidata = JSON.parse(data)
console.log(apidata)
})
})
})
You're facing this problem because response.on('data', ...
is a stream which means you likely don't have the full json object yet, which also means you're trying to parse a partial response. You should collect the json chunks in on('data', ...
and then parse the json when the response is complete. Something like this:
https.get(url, function(response){
console.log(response.statusCode);
let rawData = ''
response.on("data", function(data){
rawData = `${rawData}${data}`
})
response.on("end", function() {
const apidata = JSON.parse(rawData)
console.log(apidata)
})
})
Response from https.get are already JSON object, you don't need to parse it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.