【问题标题】:SoundCloud API: same call, different response between curl and NodeJSSoundCloud API:相同的调用,curl 和 NodeJS 之间的不同响应
【发布时间】:2016-10-20 21:58:39
【问题描述】:

我在调用 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}

使用“请求”模块从 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 提供?

【问题讨论】:

  • 尝试从选项对象中删除“json:true”。 (这只是我的好奇心)
  • 哇,这很奇怪!使用 json:true == 136692,没有 json = 137426,来自浏览器(空缓存)= 137277(昨天的结果相同)。现在..哪一个是正确的?你认为 json 选项会返回 chached 结果吗?
  • 是可能的。添加 'Cache-Control':'no-cache' 标头或在 url 处添加时间戳参数
  • 谢谢 :) 将随机参数添加到 url 我得到了不同的结果。我会测试几天,然后我会写回我的结果!
  • 我将节点请求查询字符串更改为如下所示: qs: { client_id: config.soundcloudApi.clientId, date: Date.now() } 我将在几天后写回结果

标签: node.js soundcloud cdn npm-request


【解决方案1】:

正如我在原始问题的 cmets 中所说,通过在 API 调用的 QueryString 中添加时间戳字段来解决问题。这样,请求将始终不同,您将始终获得最新的值。

感谢@JacopoBrovida 的提示!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-09
    • 2018-09-24
    • 2020-04-23
    • 2020-06-11
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多