Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Axios POST请求是否采用application/x-www-form-urlencoded格式?_Node.js_Axios_Ibm Cloud - Fatal编程技术网

Node.js Axios POST请求是否采用application/x-www-form-urlencoded格式?

Node.js Axios POST请求是否采用application/x-www-form-urlencoded格式?,node.js,axios,ibm-cloud,Node.js,Axios,Ibm Cloud,我正在尝试使用axios从Node.js向IBM Cloud发送此POST请求: 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-urlenc

我正在尝试使用
axios
从Node.js向IBM Cloud发送此POST请求:

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

当使用
应用程序/x-www-form-urlencoded
格式数据时,系统会这样做:

const querystring=require('querystring');
轴心柱http://something.com/stringify({foo:'bar'}));
我在信中写道:

const querystring=require('querystring');
轴心柱https://iam.cloud.ibm.com/identity/token“,querystring.stringify({
grant_type:'urn:ibm:params:oauth:grant type:apike',
apikey:'s00pers3cret'
})
)
.然后(功能(响应){
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({
方法:“post”,
网址:'https://iam.cloud.ibm.com/identity/token',
标题:{
contentType:'application/x-www-form-urlencoded',
接受:'application/json'
},
数据:querystring.stringify({
grant_type:'urn:ibm:params:oauth:grant type:apike',
apikey:'s00pers3cret'
})
})
.然后(功能(响应){
console.log(response.data);
})
.catch(error=>console.error(error));

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

授权类型值中缺少一个y,请将其更改为:

urn:ibm:params:oauth:grant-type:apikey
顺便说一下,这是一个来自IBM(而不是Axios)的与服务器相关的错误,您可以这样检查它们:

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

grant_类型值中缺少y,请将其更改为:

urn:ibm:params:oauth:grant-type:apikey
顺便说一下,这是一个来自IBM(而不是Axios)的与服务器相关的错误,您可以这样检查它们:

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