gpt4 book ai didi

node.js - 使用node.js在POST请求中发送URL编码的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 00:26:33 24 4
gpt4 key购买 nike

我无法将 url 编码字符串发送到 Stormpath /oauth/token API 端点。该字符串应如下所示:

 grant_type=password&username=<username>&password=<password>

使用 Postman,我通过选择原始/文本选项在请求正文中提供与上述字符串类似的字符串,成功命中端点并检索我想要的数据。但是当我生成代码片段时,它看起来像这样:

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);
});

那根绳子去哪儿了?我需要一些帮助来理解知道我使用 Postman 将 url 编码的字符串发送到 API 端点与在 Postman 生成的代码中看不到它之间的脱节。因为现在我不知道如何在我的实际应用程序中重现对端点的成功调用。

对我来说,我似乎应该简单地为请求提供一个 body ,但响应结果是 {"error":"invalid_request","message":"invalid_request “}。我还尝试将 url 编码字符串附加到 url,但这会返回 404 错误。

我刚刚重新开始使用 API,但对此缺乏经验。

最佳答案

表单数据需要作为对象发布,这里是一个例子:

request.post('http://service.com/upload', {form:{key:'value'}})

摘自本文档:

https://github.com/request/request#forms

希望这有帮助!

关于node.js - 使用node.js在POST请求中发送URL编码的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42565799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com