Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using React and axios for curl

Is it possible to make a curl request by using axios?

the curl string is:

curl -v 'https://developer.api.autodesk.com/authentication/v1/authenticate' --data 'client_id=1234&client_secret=1234&grant_type=client_credentials&scope=bucket:create bucket:read data:write data:read viewables:read' --header 'Content-Type: application/x-www-form-urlencoded' -k | jq '.'

I tried to do this:

getToken() {

    axios.get({
        url: 'https://developer.api.autodesk.com/authentication/v1/authenticate',
        data: {
            client_id: '1234',
            client_secret: '1234',
            grant_type : 'client_credentials',
            scope: 'data:read data:viewables'
        },
        beforeSend: function(xhr) {
             xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
        }, success: function(data){
            console.log(data)
        }
    })        
}

But with no luck - e.g. nothing happens.

I previously used the cygwin-terminal to make the curl-request and I succesfully got the response

{
 "token_type": "Bearer",
 "expires_in": 1799,
 "access_token": "eyJhbGciOiJIUzI1NiIsImtpZCI6Imp3dF9zeW1tZXRyaWNfa2V5X2RldiJ9.eyJjbGllbnRfaWQiOiJjWTFqcm1rQXhPSVptbnNsOVhYN0puVURtVEVETGNGeCIsImV4cCI6MTQ4NzU2NzgwMSwic2NvcGUiOlsiZGF0YTpyZWFkIl0sImF1ZCI6Imh0dHBzOi8vYXV0b2Rlc2suY29tL2F1ZC9qd3RleHAzMCIsImp0aSI6InJZcEZZTURyemtMOWZ1ZFdKSVVlVkxucGNWT29BTDg0dFpKbXlmZ29ORW1MakF0YVVtWktRWU1lYUR2UGlnNGsifQ.uzNexXCeu4efGPKGGhHdKxoJDXHAzLb28B2nSjrq_ys"
}

So, is this possible with React/axios?

In addition to the question, can I pass the received token to another curl request?

like image 677
ST80 Avatar asked Oct 25 '17 11:10

ST80


People also ask

Can Axios be used in React?

It allows you to fetch data and make HTTP requests. This one is the common method to communicate with the database in React. In React there is another method to communicate with the backend server and that requires the installation of a popular library Axios.

How do I add Axios to React?

First, you import React and Axios so that both can be used in the component. Then you hook into the componentDidMount lifecycle hook and perform a GET request. You use axios. get(url) with a URL from an API endpoint to get a promise which returns a response object.

Should I use Axios in react native?

You can make any HTTP calls using Axios in React Native. Similar to fetch, Axios also provides you the facility to call GET, POST, PUT, PATCH, and DELETE requests. Axios also has more options and features to call the API as compared to fetch.


1 Answers

Well it's not really "a curl request". It's an HTTP request. Curl is just the tool you use to do HTTP (and other) actions via the command line.

In your HTTP request, I can see you're using axios.get(), however you're trying to do a post request (you've got a data object you're trying to send). So you should be using axios.post(). It'd be best to check out the axios page to see the syntax for HTTP posts, including how to include the data and header objects in the post.

In answer to your second question, yes you can. In the .then() section of your first axios post, you can do another axios post using the response, e.g.

axios.post(
    ...
).then(response => {
    // do another post with response.token or whatever as the data
})
...
like image 54
Jayce444 Avatar answered Oct 26 '22 22:10

Jayce444