Post form data with axios in Node.js

后端 未结 4 1953
一个人的身影
一个人的身影 2021-02-18 15:39

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

相关标签:
4条回答
  • 2021-02-18 15:52

    From the error it seems your client_id or client_secret is incorrect. Enable debugging and share the raw request/response (after filtering credentials out).

    0 讨论(0)
  • 2021-02-18 16:03

    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)
    })
    
    0 讨论(0)
  • 2021-02-18 16:03

    It is not true! You can post data with axios using nodejs. I have done it. The problem is, if you use PHP on the server side, there is a pitfall you need to be aware of. Axios posts data in JSON format (Content-Type: application/json) PHP's standard $_POST array is not populated when this content type is used. So it will always be empty. In order to get post parameters sent via a json request, you need to use file_get_contents("http://php://input") .

    A simple PHP script on the server side would be:

    if($_SERVER['REQUEST_METHOD']==='POST' && empty($_POST)) {
     $_POST = json_decode(file_get_contents('http://php://input'));
    }
    

    Using this method you can avoid the formData dependency. You CAN post data directly from node.js.

    0 讨论(0)
  • 2021-02-18 16:04

    As for 10 June 2017, axios library does not support posting form data in Node.js. Here is the issue on GitHub - https://github.com/mzabriskie/axios/issues/789

    We had the similar problem and decided to switch to request library.

    0 讨论(0)
提交回复
热议问题