带有application / x-www-form-urlencoded格式的Node.js Axios POST请求?

托马斯·大卫·基霍

我正在尝试使用axios以下命令从Node.js发送此POST请求到IBM Cloud

curl -k -X POST \
  --header "Content-Type: application/x-www-form-urlencoded" \
  --header "Accept: application/json" \
  --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" \
  --data-urlencode "apikey=s00pers3cret" \
  "https://iam.cloud.ibm.com/identity/token"

curl请求有效。axios在另一个Node函数中遇到了好运

爱可信文件说,当使用做这个application/x-www-form-urlencoded格式的数据:

const querystring = require('querystring');
axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));

在Node中,我写道:

const querystring = require('querystring');
axios.post('https://iam.cloud.ibm.com/identity/token', querystring.stringify({
      grant_type: 'urn:ibm:params:oauth:grant-type:apike',
      apikey: 's00pers3cret'
    })
)
.then(function(response) {
    console.log(response.data);
})
.catch(error => console.error(error));

这是完整的错误响应:

{ Error: Request failed with status code 400
    at createError (/srv/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/srv/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/srv/node_modules/axios/lib/adapters/http.js:236:11)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)
  config: 
   { url: 'https://iam.cloud.ibm.com/identity/token',
     method: 'post',
     data: 'grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapike&apikey=s00pers3cret',
     headers: 
      { Accept: 'application/json, text/plain, */*',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'axios/0.19.2',
        'Content-Length': 110 },
     transformRequest: [ [Function: transformRequest] ],
     transformResponse: [ [Function: transformResponse] ],
     timeout: 0,
     adapter: [Function: httpAdapter],
     xsrfCookieName: 'XSRF-TOKEN',
     xsrfHeaderName: 'X-XSRF-TOKEN',
     maxContentLength: -1,
     validateStatus: [Function: validateStatus] },
  request: 
   ClientRequest {
     domain: 
      Domain {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        members: [Array] },
     _events: 
      { socket: [Function],
        abort: [Function],
        aborted: [Function],
        error: [Function],
        timeout: [Function],
        prefinish: [Function: requestOnPrefinish] },
     _eventsCount: 6,
     _maxListeners: undefined,
     output: [],
     outputEncodings: [],
     outputCallbacks: [],
     outputSize: 0,
     writable: true,
     _last: true,
     upgrading: false,
     chunkedEncoding: false,
     shouldKeepAlive: false,
     useChunkedEncodingByDefault: true,
     sendDate: false,
     _removedConnection: false,
     _removedContLen: false,
     _removedTE: false,
     _contentLength: null,
     _hasBody: true,
     _trailer: '',
     finished: true,
     _headerSent: true,
     socket: 
      TLSSocket {
        _tlsOptions: [Object],
        _secureEstablished: true,
        _securePending: false,
        _newSessionPending: false,
        _controlReleased: true,
        _SNICallback: null,
        servername: 'iam.cloud.ibm.com',
        npnProtocol: false,
        alpnProtocol: false,
        authorized: true,
        authorizationError: null,
        encrypted: true,
        _events: [Object],
        _eventsCount: 9,
        connecting: false,
        _hadError: false,
        _handle: [Object],
        _parent: null,
        _host: 'iam.cloud.ibm.com',
        _readableState: [Object],
        readable: true,
        domain: [Object],
        _maxListeners: undefined,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: false,
        _bytesDispatched: 326,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: undefined,
        _server: null,
        ssl: [Object],
        _requestCert: true,
        _rejectUnauthorized: true,
        parser: null,
        _httpMessage: [Circular],
        [Symbol(asyncId)]: 66,
        [Symbol(bytesRead)]: 0 },
     connection: 
      TLSSocket {
        _tlsOptions: [Object],
        _secureEstablished: true,
        _securePending: false,
        _newSessionPending: false,
        _controlReleased: true,
        _SNICallback: null,
        servername: 'iam.cloud.ibm.com',
        npnProtocol: false,
        alpnProtocol: false,
        authorized: true,
        authorizationError: null,
        encrypted: true,
        _events: [Object],
        _eventsCount: 9,
        connecting: false,
        _hadError: false,
        _handle: [Object],
        _parent: null,
        _host: 'iam.cloud.ibm.com',
        _readableState: [Object],
        readable: true,
        domain: [Object],
        _maxListeners: undefined,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: false,
        _bytesDispatched: 326,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: undefined,
        _server: null,
        ssl: [Object],
        _requestCert: true,
        _rejectUnauthorized: true,
        parser: null,
        _httpMessage: [Circular],
        [Symbol(asyncId)]: 66,
        [Symbol(bytesRead)]: 0 },
     _header: 'POST /identity/token HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nContent-Type: application/x-www-form-urlencoded\r\nUser-Agent: axios/0.19.2\r\nContent-Length: 110\r\nHost: iam.cloud.ibm.com\r\nConnection: close\r\n\r\n',
     _onPendingData: [Function: noopPendingOutput],
     agent: 
      Agent {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,

我也尝试过:

    const axios = require('axios');
    const querystring = require('querystring');

    axios({
      method: 'post',
      url: 'https://iam.cloud.ibm.com/identity/token',
      headers: {
        contentType: 'application/x-www-form-urlencoded',
        accept: 'application/json'
      },
      data: querystring.stringify({
        grant_type: 'urn:ibm:params:oauth:grant-type:apike',
        apikey: 's00pers3cret'
      })
    })
    .then(function(response) {
      console.log(response.data);
    })
    .catch(error => console.error(error));

错误响应相同。有什么建议?

grant_type值中缺少y,请将其更改为:

urn:ibm:params:oauth:grant-type:apikey

顺便说一下,这是来自IBM(不是Axios)的服务器相关错误,您可以像这样检查它们:

.catch(error => console.log(error.response.data));

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用application / x-www-form-urlencoded使用node.js在发布请求中发送数组

来自分类Dev

如何使用带有 loopj-async-http 库的 x-www-form-urlencoded 主体发送 post 请求

来自分类Dev

泽西岛客户发布带有x-www-form-urlencoded失败的请求失败

来自分类Dev

如何发送带有内容类型x-www-form-urlencoded android改造的发布请求

来自分类Dev

如何在内容类型 application/x-www-form-urlencoded 中使用自定义对象测试 Post 请求?

来自分类Dev

PowerShell - Slack API - 房间历史 - 带有 x-www-form-urlencoded 参数的 Post 方法

来自分类Dev

如何使Angular 2将所有请求发送为application / x-www-form-urlencoded

来自分类Dev

如何在内容类型='application / x-www-form-urlencoded'的node.js中发布数据

来自分类Dev

file_get_contents('php:// input'); 带有application / x-www-form-urlencoded;

来自分类Dev

如何在webclient的post请求中发送x-www-form-urlencoded?

来自分类Dev

此资源不支持请求实体的媒体类型'application / x-www-form-urlencoded'

来自分类Dev

如何使用Fetch发布x-www-form-urlencoded请求?

来自分类Dev

Javascript:从x-www-form-urlencoded请求获取参数值

来自分类Dev

使用POST + application / x-www-form-urlencoded发送敏感数据

来自分类Dev

RestSharp在POST上默认将Content-Type设置为application / x-www-form-urlencoded

来自分类Dev

Angularjs $http.post 联系表单 7,内容类型为 application/x-www-form-urlencoded

来自分类Dev

Akka HTTP 如何使用 Content-Type application/x-www-form-urlencoded POST singleRequest

来自分类Dev

导入OpenAPI 2.0 Postman集合,所有POST请求正文均显示为x-www-form-urlencoded,而不是原始JSON

来自分类Dev

带角度的http帖子,带有“ Content-Type”:“ application / x-www-form-urlencoded”和responseType:“ text”

来自分类Dev

解析ExpressJS中的application / x-www-form-urlencoded

来自分类Dev

ModelAttribute未映射application / x-www-form-urlencoded

来自分类Dev

Jsoup发送application / x-www-form-urlencoded而不是json

来自分类Dev

为什么 Volley String 请求不能在 x-www-form-urlencoded 中使用 POST 方法发送 Body 参数?

来自分类Dev

multipart / form-data和application-x-www-form-urlencoded有什么区别?

来自分类Dev

无法使用带有x-www-form-urlencoded参数的JSOUP登录网站

来自分类Dev

Spring MVC-@RequestParam导致带有x-www-form-urlencoded的MissingServletRequestParameterException

来自分类Dev

如何使用带有角度的ngResource模块发送x-www-form-urlencoded数据?

来自分类Dev

无法使用带有x-www-form-urlencoded参数的JSOUP登录网站

来自分类Dev

PYTHON:requests.post()如何发送编码为application / x-www-form-urlencoded的request_body

Related 相关文章

  1. 1

    使用application / x-www-form-urlencoded使用node.js在发布请求中发送数组

  2. 2

    如何使用带有 loopj-async-http 库的 x-www-form-urlencoded 主体发送 post 请求

  3. 3

    泽西岛客户发布带有x-www-form-urlencoded失败的请求失败

  4. 4

    如何发送带有内容类型x-www-form-urlencoded android改造的发布请求

  5. 5

    如何在内容类型 application/x-www-form-urlencoded 中使用自定义对象测试 Post 请求?

  6. 6

    PowerShell - Slack API - 房间历史 - 带有 x-www-form-urlencoded 参数的 Post 方法

  7. 7

    如何使Angular 2将所有请求发送为application / x-www-form-urlencoded

  8. 8

    如何在内容类型='application / x-www-form-urlencoded'的node.js中发布数据

  9. 9

    file_get_contents('php:// input'); 带有application / x-www-form-urlencoded;

  10. 10

    如何在webclient的post请求中发送x-www-form-urlencoded?

  11. 11

    此资源不支持请求实体的媒体类型'application / x-www-form-urlencoded'

  12. 12

    如何使用Fetch发布x-www-form-urlencoded请求?

  13. 13

    Javascript:从x-www-form-urlencoded请求获取参数值

  14. 14

    使用POST + application / x-www-form-urlencoded发送敏感数据

  15. 15

    RestSharp在POST上默认将Content-Type设置为application / x-www-form-urlencoded

  16. 16

    Angularjs $http.post 联系表单 7,内容类型为 application/x-www-form-urlencoded

  17. 17

    Akka HTTP 如何使用 Content-Type application/x-www-form-urlencoded POST singleRequest

  18. 18

    导入OpenAPI 2.0 Postman集合,所有POST请求正文均显示为x-www-form-urlencoded,而不是原始JSON

  19. 19

    带角度的http帖子,带有“ Content-Type”:“ application / x-www-form-urlencoded”和responseType:“ text”

  20. 20

    解析ExpressJS中的application / x-www-form-urlencoded

  21. 21

    ModelAttribute未映射application / x-www-form-urlencoded

  22. 22

    Jsoup发送application / x-www-form-urlencoded而不是json

  23. 23

    为什么 Volley String 请求不能在 x-www-form-urlencoded 中使用 POST 方法发送 Body 参数?

  24. 24

    multipart / form-data和application-x-www-form-urlencoded有什么区别?

  25. 25

    无法使用带有x-www-form-urlencoded参数的JSOUP登录网站

  26. 26

    Spring MVC-@RequestParam导致带有x-www-form-urlencoded的MissingServletRequestParameterException

  27. 27

    如何使用带有角度的ngResource模块发送x-www-form-urlencoded数据?

  28. 28

    无法使用带有x-www-form-urlencoded参数的JSOUP登录网站

  29. 29

    PYTHON:requests.post()如何发送编码为application / x-www-form-urlencoded的request_body

热门标签

归档