SoundCloud API:相同的调用,curl 和 NodeJS 之间的响应不同
SoundCloud API: same call, different response between curl and NodeJS
我在调用 SoundCloud API 时遇到问题:对于来自同一服务器的相同 API 调用,我在执行 CURL 请求和 NodeJs 之间得到不同的结果。以下两个调用在 2 秒内相互调用。
从我们的服务器调用 "SiaMusic":curl http://api.soundcloud.com/users/3789802?client_id=MY_CLIENT_ID
响应:{"id":3789802,"kind":"user","permalink":"siamusic","username":"SiaMusic","last_modified":"2016/03/27 21:00:51 +0000","uri":"https://api.soundcloud.com/users/3789802","permalink_url":"http://soundcloud.com/siamusic","avatar_url":"https://i1.sndcdn.com/avatars-000073011508-o3ui3y-large.jpg","country":null,"first_name":"","last_name":"","full_name":"","description":"","city":"","discogs_name":null,"myspace_name":null,"website":"http://siamusic.net/","website_title":"","track_count":204,"playlist_count":23,"online":false,"plan":"Pro Plus","public_favorites_count":1,"subscriptions":[{"product":{"id":"creator-pro-unlimited","name":"Pro Unlimited"}}],"followers_count":137277,"followings_count":0}
使用 "request" 模块从 NodeJs 调用:
var options = {
method: 'GET',
url: 'http://api.soundcloud.com/users/3789802',
qs: {client_id: 'MY_CLIENT_ID' },
json: true
};
request(options, function(err, response, body) {
//handle error, if body
console.log(body);
}
响应:正文:{ id: 3789802, kind: 'user', permalink: 'siamusic', username: 'SiaMusic', last_modified: '2016/03/27 21:00:51 +0000', uri: 'https://api.soundcloud.com/users/3789802', permalink_url: 'http://soundcloud.com/siamusic', avatar_url: 'https://i1.sndcdn.com/avatars-000073011508-o3ui3y-large.jpg', country: null, first_name: '', last_name: '', full_name: '', description: '', city: '', discogs_name: null, myspace_name: null, website: 'http://siamusic.net/', website_title: '', track_count: 204, playlist_count: 23, online: false, plan: 'Pro Plus', public_favorites_count: 1, subscriptions: [ [Object] ], followers_count: 136692, followings_count: 0 }}
正如您从 JSON 中看到的那样,"followers_count" 有很大不同,而且(对于 NodeJs 调用)我在 5 到 30 天内得到了相同的结果。 API 是否由 CDN 提供服务?
正如我在原始问题的评论中所说,通过在 API 调用的 QueryString 中添加时间戳字段来解决问题。这样请求将始终不同,您将始终获得最新的值。
感谢@JacopoBrovida 的提示!
我在调用 SoundCloud API 时遇到问题:对于来自同一服务器的相同 API 调用,我在执行 CURL 请求和 NodeJs 之间得到不同的结果。以下两个调用在 2 秒内相互调用。
从我们的服务器调用 "SiaMusic":curl http://api.soundcloud.com/users/3789802?client_id=MY_CLIENT_ID
响应:{"id":3789802,"kind":"user","permalink":"siamusic","username":"SiaMusic","last_modified":"2016/03/27 21:00:51 +0000","uri":"https://api.soundcloud.com/users/3789802","permalink_url":"http://soundcloud.com/siamusic","avatar_url":"https://i1.sndcdn.com/avatars-000073011508-o3ui3y-large.jpg","country":null,"first_name":"","last_name":"","full_name":"","description":"","city":"","discogs_name":null,"myspace_name":null,"website":"http://siamusic.net/","website_title":"","track_count":204,"playlist_count":23,"online":false,"plan":"Pro Plus","public_favorites_count":1,"subscriptions":[{"product":{"id":"creator-pro-unlimited","name":"Pro Unlimited"}}],"followers_count":137277,"followings_count":0}
使用 "request" 模块从 NodeJs 调用:
var options = {
method: 'GET',
url: 'http://api.soundcloud.com/users/3789802',
qs: {client_id: 'MY_CLIENT_ID' },
json: true
};
request(options, function(err, response, body) {
//handle error, if body
console.log(body);
}
响应:正文:{ id: 3789802, kind: 'user', permalink: 'siamusic', username: 'SiaMusic', last_modified: '2016/03/27 21:00:51 +0000', uri: 'https://api.soundcloud.com/users/3789802', permalink_url: 'http://soundcloud.com/siamusic', avatar_url: 'https://i1.sndcdn.com/avatars-000073011508-o3ui3y-large.jpg', country: null, first_name: '', last_name: '', full_name: '', description: '', city: '', discogs_name: null, myspace_name: null, website: 'http://siamusic.net/', website_title: '', track_count: 204, playlist_count: 23, online: false, plan: 'Pro Plus', public_favorites_count: 1, subscriptions: [ [Object] ], followers_count: 136692, followings_count: 0 }}
正如您从 JSON 中看到的那样,"followers_count" 有很大不同,而且(对于 NodeJs 调用)我在 5 到 30 天内得到了相同的结果。 API 是否由 CDN 提供服务?
正如我在原始问题的评论中所说,通过在 API 调用的 QueryString 中添加时间戳字段来解决问题。这样请求将始终不同,您将始终获得最新的值。
感谢@JacopoBrovida 的提示!