简体   繁体   中英

Can not make Post request in react

I'm trying to make a POST in react, and I have an error that says {"error_description":"Missing grant type"} in postman works fine, what am I doing wrong? Thanks!

Here is my code

class App extends Component {
  constructor() {
    super()
    this.state = {
      info : null
    }
  }

  componentDidMount() {
    var payload = {
      client_id: 'my_site',
      grant_type: 'my_credentials',
      client_secret: 'xxx',   
    }
    var data = new FormData();
    data.append("json",JSON.stringify(payload));

     fetch('/myendpoint', {
        method: "POST",
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
        },
        body: data
        })
   })
    .then(function(res){ 
      return res.json(); 
    })
    .then(function(data){ 
      alert( JSON.stringify( data ) ) 
    }) 

When you make the post request define the type when: URL Encoded but you send a JSON

Try to make the request with the next format:

 fetch('/myendpoint?client_id="some id"&grant_type="some type"', { method: "POST", headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } }) 

Because your values its on the URL Make the post without body

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM