react-nativeapiaxiostwitchtwitch-api

Problem with API of Twitch with Axios GET POST


i got problem with codes.

axios({
        method: 'post',
        url: 'https://id.twitch.tv/oauth2/token',
        body: {
        client_id: 'a',
        client_secret: 'Bearer b',
        grant_type: 'client_credentials',}
            }).then(response => {
        console.log(response);

      })
      .catch(error => {
        console.log(error);
      })

and result is :

data: { status: 400, message: 'missing client id' }

but this way works fine if i put them in url :

url: 'https://id.twitch.tv/oauth2/token?client_id=a&client_secret=b&grant_type=client_credentials',

what is my problem? also can u give me an example for axios.get ? with :

url: 'https://api.twitch.tv/helix/games/top'
headers: {
client_id: 'a',
Authorization: 'Bearer b',
}

Solution

  • For axios you need to be sending as data not body

    And then construct/send a FormData

    So for example

    axios({
      method: "post",
      url: "myurl",
      data: bodyFormData,
      headers: { "Content-Type": "multipart/form-data" },
    })
      .then(function (response) {
        //handle success
        console.log(response);
      })
      .catch(function (response) {
        //handle error
        console.log(response);
      });
    

    See also: axios post request to send form data and: https://github.com/axios/axios/issues/318

    Using body will get mixed results as the library (axios) won't know how to encode the data being sent

    Personally myself I'm using got over axios so I send

                got({
                    url: "https://id.twitch.tv/oauth2/token",
                    method: "POST",
                    headers: {
                        "Accept": "application/json"
                    },
                    form: {
                        client_id: config.client_id,
                        client_secret: config.client_secret,
                        grant_type: "client_credentials"
                    },
                    responseType: "json"
                })
                .then(resp => {
                    //SNIP