Has anyone had trouble setting a Content-Type
header using apollo-datasource-rest
? I'm trying to encode for application/x-www-form-urlencoded
and my REST API is still not seeing the params:
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 ))
}
}
Output:
// 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'
}
It appears the request (POST) body is formatted correctly and that the headers are set correctly. Using the same credentials and headers via postman returns a successful result, but not via this library for some reason:
// response
{ success: 0,
error:
{ status: 400,
message: 'Missing username or password',
code: 117
}
}
It's maybe a little bit late but i had the same problem before. You need to put the params as query string if you want to use application/x-www-form-urlencoded
, for example
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 ))
}
}
Not a good one but it's should works
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.