javascript - 使用 Node 创建 StrawPoll.me 民意调查

标签 javascript node.js api post request

我试图使用 Strawpoll.me API 创建一个新的民意调查。

var poll = { title: 'test', options: [ '1', '2' ] };

var request = require('request');

request.post(
    'https://strawpoll.me/api/v2/polls',
    {
        body: poll,
        json: true
    },
    function (error, response, body) {
        console.log(JSON.stringify(response, null, 2));
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);

但我无法创建新的民意调查。我收到“statusCode”:307 和空正文。

StrawPoll 文档:https://github.com/strawpoll/strawpoll/wiki/API

我能用它做什么?谢谢!

最佳答案

添加此选项followAllRedirects: true,

var poll = { title: 'test', options: [ '1', '2' ] };

var request = require('request');

request.post({
  url: 'https://strawpoll.me/api/v2/polls',
  followAllRedirects: true, // <----
  body: poll,
  json: true
},
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);

回应:

{ id: 10585477,
  title: 'test',
  options: [ '1', '2' ],
  votes: [ 0, 0 ],
  multi: false,
  dupcheck: 'normal',
  captcha: false }

关于javascript - 使用 Node 创建 StrawPoll.me 民意调查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37950669/

相关文章:

node.js - Mongoose 如果有新引用则保存

authentication - 建立套接字后的socket.io身份验证

node.js - 如何为 Function APP 生成 Azure Swagger UI

javascript - Ext JS 的自定义缓存设置

javascript - 在javascript中将值转换为bytearray

javascript - 向客户端发送ajax错误问题

php - 如何使用 CSRF token 发布到 Laravel API?

javascript - 当循环在异步函数内部而不是相反时,为什么 Async/Await 可以正常工作?

javascript - Accept Header在javascript中的应用

python - 如何使用参数化过滤创建类似 Twitter 的流 API?