简体   繁体   中英

Json http.get nodejs gives wrong data

Good day, I am trying to fetch a json file with http.get

 http.get("http://steamcommunity.com/profiles/76561197993482001"+ "/inventory/json/730/2",function(res){ var chunk2=''; res.on('data',function(chunk){ chunk2+=chunk; }); res.on('end',function(){ console.log(chunk2); }); }); 

But in my console there are only these 5 strange characters: (and some squares with numbers in it)

What is going on? Thank you

EDIT I tried adding res.setEncoding('utf8'); but it doesnt work

The URL in question returns a 302 (redirect) status code, so you should check res.statusCode and reissue the request using the provided Location header in the response (which points to the actual location). Alternatively, you could use the request module which will handle redirects for you.

As for the data you're getting: the server does return a content body for the 302 response, and that content consists of a 26 byte gzipped document that doesn't seem to contain anything:

$ curl --silent --no-location 'http://steamcommunity.com/profiles/76561197993482001/inventory/json/730/2' | gunzip | wc -c
       0

Since Node interprets the response as UTF-8 (which it isn't), you get those strange characters.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM