使用 React 和 axios 进行 curl

Using React and axios for curl

是否可以使用 axios 发出 curl 请求?

curl 字符串是:

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 '.'

我试过这样做:

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)
        }
    })        
}

但没有运气 - 例如没有任何反应。

我之前使用 cygwin-terminal 发出 curl-request,我成功地得到了响应

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

那么,React/axios 有可能吗?

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

嗯,这不是真的 "a curl request"。这是一个 HTTP 请求。 Curl 只是您用来通过命令行执行 HTTP(和其他)操作的工具。

在您的 HTTP 请求中,我可以看到您正在使用 axios.get(),但是您正在尝试执行 post 请求(您有一个数据 object正在尝试发送)。所以你应该使用 axios.post()。最好查看 axios page 以查看 HTTP posts 的语法,包括如何在 [=24] 中包含数据和 header objects =].

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

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