![](/img/trans.png)
[英]How to post a HTML form using Javascript that has both “application/x-www-form-urlencoded” and “charset=UTF-8” in the Content-Type header
[英]Setting Content-Type header to application/x-www-form-urlencoded using apollo-datasource-rest library
有沒有人在使用apollo-datasource-rest
設置Content-Type
標頭時遇到問題? 我正在嘗試對application/x-www-form-urlencoded
,但我的 REST API 仍然沒有看到參數:
class AuthAPI extends RESTDataSource {
...
willSendRequest( request ) {
request.headers.set( 'X-API-KEY', this.apiKey )
request.headers.set( 'Content-Type', 'application/x-www-form-urlencoded')
console.log( request.headers )
console.log( request.body )
}
async getToken( params ) {
return this
.post( apiEndpoints.auth.token, params )
.catch( err => handleError( err ))
}
}
輸出:
// console.log( request.headers )
Headers {
[Symbol(map)]: [Object: null prototype] {
'X-API-KEY': [ '1234567890...' ],
'Content-Type': [ 'application/x-www-form-urlencoded' ]
}
}
// console.log( request.body )
{
identifier: 'my.name@domain.com',
format: 'json',
secret: 'P@55w0rd'
}
看來請求 (POST) 正文格式正確並且標頭設置正確。 通過郵遞員使用相同的憑據和標頭返回一個成功的結果,但由於某種原因不能通過這個庫:
// response
{ success: 0,
error:
{ status: 400,
message: 'Missing username or password',
code: 117
}
}
可能有點晚了,但我以前也遇到過同樣的問題。 例如,如果要使用application/x-www-form-urlencoded
,則需要將參數作為查詢字符串
class AuthAPI extends RESTDataSource {
...
willSendRequest( request ) {
request.headers.set( 'X-API-KEY', this.apiKey )
console.log( request.headers )
console.log( request.body )
}
async getToken( params ) {
return this
.post(
apiEndpoints.auth.token,
'loginId=myloginId&password=12345678',
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
)
.catch( err => handleError( err ))
}
}
不是很好,但它應該有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.