Search code examples
node.jsaxiosibm-cloud

Node.js Axios POST request with application/x-www-form-urlencoded format?


I'm trying to send this POST request to IBM Cloud from Node.js using axios:

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"

The curl request works. I've had good luck with axios in another Node function.

The axios documentation says to do this when using application/x-www-form-urlencoded format data:

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

In Node I wrote:

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

Here's the full error response:

{ 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,

I also tried:

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

Same error response. Any suggestions?


Solution

  • There is a missing y in grant_type value, change it to this:

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

    By the way, this is a server related error coming from IBM (not Axios), you can inspect them like this:

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