javascript 使用 React 和 axios 进行 curl

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46931008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:02:35  来源:igfitidea点击:

Using React and axios for curl

javascriptreactjscurlaxios

提问by ST80

Is it possible to make a curlrequest by using axios?

是否可以curl通过使用提出请求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

我以前使用cygwin-terminal 来发出curl-request 并且我成功地得到了响应

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

So, is this possible with React/axios?

那么,这可以通过 React/axios 实现吗?

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

除了问题,我可以将收到的令牌传递给另一个curl请求吗?

回答by Jayce444

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.

好吧,这并不是真正的“卷曲请求”。这是一个 HTTP 请求。Curl 只是您用来通过命令行执行 HTTP(和其他)操作的工具。

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 pageto see the syntax for HTTP posts, including how to include the data and header objects in the post.

在您的 HTTP 请求中,我可以看到您正在使用axios.get(),但是您正在尝试执行发布请求(您有一个要发送的数据对象)。所以你应该使用axios.post(). 最好查看axios 页面以查看 HTTP 帖子的语法,包括如何在帖子中包含数据和标头对象。

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.

回答你的第二个问题,是的,你可以。在.then()您的第一个 axios 帖子的部分中,您可以使用响应进行另一个 axios 帖子,例如

axios.post(
    ...
).then(response => {
    // do another post with response.token or whatever as the data
})
...