如何使用 Vue JS-axios 在 POST api 中传递 Header + Body
how to pass Header + Body in POST api using Vue JS-axios
我正在尝试调用 aws Cognito (Token endpoint) 的 post API。它在我的 postman 客户端中运行良好。但是我在我的 VueJS 代码中遇到了这个问题。
下面是我的代码片段。
test.vue
<script>
HTTP.post(`token`, {
'grant_type': 'authorization_code',
'client_id': 'XXXXXXXXXXXXXXXXXXXXXXXXX',
'redirect_uri': 'http://localhost:8080/callback',
'code': this.$route.query.code
})
.then(response => {
console.log('Response: ' + response)
})
.catch(e => {
console.log('Error: ' + e)
})
</script>
我成功地从 Login Endpoint 获得了 "code" 值
在上面的代码中,HTTP 是从下面的其他 javascript 导入的 object。
http-common.js
import axios from 'axios'
export const HTTP = axios.create({
baseURL: 'https://maddox.auth.eu-west-1.amazoncognito.com/oauth2/',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
我不确定,但问题是在我的 post 人中,我在 body + header 中使用了 'application/x-www-form-urlencoded' 选项。在这里我无法在 body.
中设置此值
我认为 body 中的 header 和 'application/x-www-form-urlencoded' 选项设置不正确。
I have tried with {emulateJSON:true} option. But not worked!
我收到 HTTP 代码:400
{"data":{"error":"invalid_request"},"status":400,"statusText":"Bad Request","headers":{"pragma":"no-cache","content-type":"application/json;charset=UTF-8","cache-control":"no-cache, no-store, max-age=0, must-revalidate","expires":"0"},"config":{"transformRequest":{},"transformResponse":{},"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"headers":{"Accept":"application/json","Content-Type":"application/x-www-form-urlencoded"},"method":"post","baseURL":"https://maddox.auth.eu-west-1.amazoncognito.com","url":"https://maddox.auth.eu-west-1.amazoncognito.com/oauth2/token","data":"{\"grant_type\":\"authorization_code\",\"client_id\":\"4jcmshlse80ab667okni41fbf5\",\"redirect_uri\":\"http://localhost:8080/callback\",\"code\":\"e19170dc-3d8f-420e-99b6-c05f7abad313\"}"},"request":{}}
TOKEN Endpoint 只接受 application/x-www-form-urlencoded
而你正在发送 JSON (因为 axios 只将 JavaScript 对象序列化为 JSON)
如何使用axios发送application/x-www-form-urlencoded
:https://github.com/axios/axios#using-applicationx-www-form-urlencoded-format
这里是 qs
库的例子
<script>
HTTP.post(`token`, qs.stringify({
'grant_type': 'authorization_code',
'client_id': 'XXXXXXXXXXXXXXXXXXXXXXXXX',
'redirect_uri': 'http://localhost:8080/callback',
'code': this.$route.query.code
}))
.then(response => {
console.log('Response: ' + response)
})
.catch(e => {
console.log('Error: ' + e)
})
</script>
我正在尝试调用 aws Cognito (Token endpoint) 的 post API。它在我的 postman 客户端中运行良好。但是我在我的 VueJS 代码中遇到了这个问题。
下面是我的代码片段。
test.vue
<script>
HTTP.post(`token`, {
'grant_type': 'authorization_code',
'client_id': 'XXXXXXXXXXXXXXXXXXXXXXXXX',
'redirect_uri': 'http://localhost:8080/callback',
'code': this.$route.query.code
})
.then(response => {
console.log('Response: ' + response)
})
.catch(e => {
console.log('Error: ' + e)
})
</script>
我成功地从 Login Endpoint 获得了 "code" 值 在上面的代码中,HTTP 是从下面的其他 javascript 导入的 object。
http-common.js
import axios from 'axios'
export const HTTP = axios.create({
baseURL: 'https://maddox.auth.eu-west-1.amazoncognito.com/oauth2/',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
我不确定,但问题是在我的 post 人中,我在 body + header 中使用了 'application/x-www-form-urlencoded' 选项。在这里我无法在 body.
中设置此值我认为 body 中的 header 和 'application/x-www-form-urlencoded' 选项设置不正确。
I have tried with {emulateJSON:true} option. But not worked!
我收到 HTTP 代码:400
{"data":{"error":"invalid_request"},"status":400,"statusText":"Bad Request","headers":{"pragma":"no-cache","content-type":"application/json;charset=UTF-8","cache-control":"no-cache, no-store, max-age=0, must-revalidate","expires":"0"},"config":{"transformRequest":{},"transformResponse":{},"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"headers":{"Accept":"application/json","Content-Type":"application/x-www-form-urlencoded"},"method":"post","baseURL":"https://maddox.auth.eu-west-1.amazoncognito.com","url":"https://maddox.auth.eu-west-1.amazoncognito.com/oauth2/token","data":"{\"grant_type\":\"authorization_code\",\"client_id\":\"4jcmshlse80ab667okni41fbf5\",\"redirect_uri\":\"http://localhost:8080/callback\",\"code\":\"e19170dc-3d8f-420e-99b6-c05f7abad313\"}"},"request":{}}
TOKEN Endpoint 只接受 application/x-www-form-urlencoded
而你正在发送 JSON (因为 axios 只将 JavaScript 对象序列化为 JSON)
如何使用axios发送application/x-www-form-urlencoded
:https://github.com/axios/axios#using-applicationx-www-form-urlencoded-format
这里是 qs
库的例子
<script>
HTTP.post(`token`, qs.stringify({
'grant_type': 'authorization_code',
'client_id': 'XXXXXXXXXXXXXXXXXXXXXXXXX',
'redirect_uri': 'http://localhost:8080/callback',
'code': this.$route.query.code
}))
.then(response => {
console.log('Response: ' + response)
})
.catch(e => {
console.log('Error: ' + e)
})
</script>