0

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 ) ) 
    }) 
Phil
  • 899
  • 1
  • 10
  • 20

1 Answers1

0

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

Alexis Olveres
  • 161
  • 3
  • 11