简体   繁体   English

如何将此卷曲请求转换为Javascript

[英]How to Convert This Curl Request to Javascript

I'm using Streamlabs API to update loyalty points for users. 我正在使用Streamlabs API更新用户的忠诚度积分。 After executing the below, points are not updated and an error is thrown to the catch block. 执行以下操作后,点不会更新,并且会向catch块引发错误。 The only example given for doing this request is with a curl request. 为此请求给出的唯一示例是curl请求。 (see here ). (请参阅此处 )。 Maybe their example is wrong? 也许他们的榜样是错误的?

I'm using Javascript and the Request-Promise module. 我正在使用Javascript和Request-Promise模块。 I've successfully done POST requests with this module for Oauth with Twitch. 我已经成功地使用Twitch使用此模块为Oauth完成了POST请求。 However I cannot get this post to Streamlabs API to work, I've tried a few ways. 但是,我无法将此帖子发布到Streamlabs API上,我尝试了几种方法。 This is the same code I used for a POST request to the Twitch API and it was successful. 这是我用于Twitch API的POST请求的相同代码,并且成功。 I cannot figure out why it's not working for Streamlabs: 我无法弄清楚为什么它不适用于Streamlabs:

      const options = {
        access_token: streamlabsToken,
        channel: TWITCH_CHANNEL,
        users: 'users[' + username + ']=' + REFERRAL_POINTS
      }


      request.post(pointsAddEndpoint, { json: options })
        .then((slResponse) => {                                                       // Then 2
            if (slResponse.hasOwnProperty('message')) {
              console.log('Streamlabs response: ' + slResponse.message);
            }
            res.end();
        })
      .catch(function (error) {
        console.log('Error adding refer points: ' + error.message);
        res.end();
      });

I'm sure I'm just formatting something wrong here, any input? 我确定我只是在这里格式化错误的内容,有输入吗?

UPDATE UPDATE

I got a curl request to work. 我接到要求去上班的请求。 The parameters for 'users' shouldn't be 'users=users[username]=20&users[username]=30' it should be just 'users[username]=20&users[username]=30' (removed the 'users='). “用户”的参数不应为“用户=用户[用户名] = 20&用户[用户名] = 30”,而应仅为“用户[用户名] = 20&用户[用户名] = 30”(已删除“用户=”) 。

I did this in my options object like this: 我是这样在我的options对象中做到的:

      const options = {
        access_token: streamlabsToken,
        channel: TWITCH_CHANNEL
      }

      options['users[' + username + ']'] = REFERRAL_POINTS;

However this still doesn't work. 但是,这仍然行不通。 I know I'm setting the 'users' param correctly because I printed to the log options['users[' + username + ']'] and it gave me the correct REFERRAL_POINTS. 我知道我正确设置了“用户”参数,因为我打印了日志options['users[' + username + ']'] ,它给了我正确的REFERRAL_POINTS。

At this point I'm thinking I'll just type the whole thing out as a string. 在这一点上,我正在考虑将整个内容输入为字符串。 How do I pass that into the body of the request.post ? 如何将其传递到request.post的正文中?

const options = {
    method: 'POST',
    uri: 'https://streamlabs.com/api/v1.0/points/add', // whatever the endpoint
    body: {
        access_token: streamlabsToken,
        channel: TWITCH_CHANNEL,
        users: 'users[' + username + ']=' + REFERRAL_POINTS
    },
    json: true // Automatically stringifies the body to JSON
};


request(options)
.then((parsedBody) => {
    // POST succeeded...
})
.catch((err) => {
    // POST failed...
});

So turns out the API will not accept body parameters, even though the API reference says that's how you should do it. 因此,即使API参考说明您应该这样做,该API仍不接受主体参数。 In fact they should be query parameters. 实际上,它们应该是查询参数。 I guess the API reference is incorrect. 我猜API参考不正确。 I added the params directly into the url, and put the Streamlabs OAuth access token in the header, then it worked! 我将参数直接添加到url中,并将Streamlabs OAuth访问令牌放在标题中,然后它起作用了!

      var dataString = 'users[' + username + ']=' + REFERRAL_POINTS + '&channel=' + TWITCH_CHANNEL;

      const options = {
          method: 'POST',
          url: 'https://streamlabs.com/api/v1.0/points/add' + '?' + dataString,
          headers: {
            'Authorization': 'Bearer ' + streamlabsToken
          },
          json: true
      };


      request.post(options)
        .then((slResponse) => { 
          // Do something                                                      
        })
       .catch(function (error) {
          // Do something
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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