在POST请求中使用node.js发送url编码的string
下午好,
我很难发送一个url编码的string到Stormpath /oauth/token
API端点。 string是这样的:
grant_type=password&username=<username>&password=<password>
使用Postman我通过select原始/文本选项,在请求正文中提供了一个与上面类似的string,从而成功地敲击了端点并获取了我想要的数据。 但是当我生成代码片段时,它看起来像这样:
var request = require("request"); var options = { method: 'POST', url: 'https://<My DNS label>.apps.stormpath.io/oauth/token', headers: { 'postman-token': '<token>', 'cache-control': 'no-cache', 'content-type': 'application/x-www-form-urlencoded', host: '<My DNS label>.apps.stormpath.io', accept: 'application/json' }, form: false }; request(options, function (error, response, body) { if (error) throw new Error(error); console.log(body); });
那串去哪了? 我想了解一些帮助,了解之间的断开知道我发送一个URL编码的string到API端点使用邮递员,而不是在邮政局生成的代码中看到它。 因为现在我不知道如何在我的实际应用程序中重现对端点的成功调用。
对我来说,似乎我应该简单地提供一个请求的body
,但答复出来是{"error":"invalid_request","message":"invalid_request"}
。 我也尝试追加url编码的string到url,但是返回一个404错误。
我感谢任何帮助,因为我现在正在重新使用一个API,而且我不是很有经验。
表单数据需要作为对象发布,这里是一个例子:
request.post('http://service.com/upload', {form:{key:'value'}})
从此文档中获取:
https://github.com/request/request#forms
希望这可以帮助!