Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post form data with axios in Node.js

Tags:

I'm testing out the Uber API on Postman, and I'm able to send a request with form data successfully. When I try to translate this request using Node.js and the axios library I get an error.

Here is what my Postman request looks like:

Postman POST request

The response I get is: { "error": "invalid_client" }

Here is what I'm doing in Node.js and axios:

var axios = require("axios");  const config = { headers: { 'Content-Type': 'multipart/form-data' } };  axios.post('https://login.uber.com/oauth/v2/token', {   client_id: '***',   client_secret: '***',   grant_type: 'authorization_code',   redirect_uri: 'http://localhost:8080/',   code: '***' }, config)   .then(function(response) {     console.log(response.data)   })   .catch(function(error) {     console.log(error)   }) 

When I do this, I get a 400 response.

I added the 'multipart/form-data' header because I filled out the form-data in the Postman request. Without the header I get the same result.

I'm expecting to get the same response I'm getting from Postman, is there something wrong with my config variable in the Node.js script?

Any help would be appreciated!

like image 384
Mike Avatar asked Jan 20 '17 12:01

Mike


People also ask

How do I post data on Axios?

To perform an HTTP POST request in Axios, call axios.post() . Making a POST request in Axios requires two parameters: the URI of the service endpoint and an object that contains the properties you wish to send to the server. For a simple Axios POST request, the object must have a url property.

How send form data in Axios put request?

Sending a PUT Request with Axios The simplest way to make the PUT call is to simply use the put() function of the axios instance, and supply the body of that request in the form of a JavaScript object: const res = await axios. put('/api/article/123', { title: 'Making PUT Requests with Axios', status: 'published' });

How do I send a POST request from Axios?

A POST request can be made using Axios to “post” data to an endpoint. This endpoint may then use this POST request to perform a certain task or trigger an event. The HTTP post request is performed by calling axios. post() .


1 Answers

You might be able to use Content-Type: 'application/x-www-form-urlencoded'. I ran into a similar issue with https://login.microsoftonline.com where it was unable to handle incoming application/json.

var axios = require("axios");  axios({   url: 'https://login.uber.com/oauth/v2/token',   headers: { 'Content-Type': 'application/x-www-form-urlencoded' },   data: `client_id=${encodeURIComponent('**')}&client_secret=${encodeURIComponent('**')}&grant_type=authorization_code&redirect_uri=${encodeURIComponent('http://localhost:8080/')}&code=${encodeURIComponent('**')}` }) .then(function(response) {   console.log(response.data) }) .catch(function(error) {   console.log(error) }) 

You could also use a function to handle the translation to formUrlEncoded like so

const formUrlEncoded = x =>    Object.keys(x).reduce((p, c) => p + `&${c}=${encodeURIComponent(x[c])}`, '')  var axios = require("axios");  axios({   url: 'https://login.uber.com/oauth/v2/token',   headers: { 'Content-Type': 'application/x-www-form-urlencoded' },   data: formUrlEncoded({      client_id: '***',      client_secret: '***',      grant_type: 'authorization_code',      redirect_uri: 'http://localhost:8080/',      code: '***'    }) }) .then(function(response) {   console.log(response.data) }) .catch(function(error) {   console.log(error) }) 
like image 131
Reid Evans Avatar answered Sep 21 '22 02:09

Reid Evans