简体   繁体   中英

Pocket authentication returns BadRequest with content-type 'application/json'

I am retrieving access token for pocket api. I am able to do so successfully using a Http POST request with content type as application/x-www-form-urlencoded .

{ 
  host: 'getpocket.com',
  path: '/v3/oauth/authorize',
  port: 443,
  method: 'POST',
  headers:
   { 'Content-Type': 'application/x-www-form-urlencoded',
     'Content-Length': 79 } 
}

But pocket also supports content type as application/json .

{ 
  host: 'getpocket.com',
  path: '/v3/oauth/authorize',
  port: 443,
  method: 'POST',
  headers: { 'Content-Type': 'application/json', 'Content-Length': 79 } 
}

But using this type of request returns me

'400 Bad Request'

I am doing this on nodejs. Do i have to pass any extra details on this, like 'X-Accept'(dont know how do so).

I think Pocket is reporting a bad request because you are sending form encoded data while declaring it as JSON in Content-Type Header.

If you want to send data to Pocket in JSON format, set Content-Type: application/json; charset=UTF8 Content-Type: application/json; charset=UTF8 .

If you want to receive data in JSON format set X-Accept: application/json .

To include custom headers in HTTP requests, just add the name value pair to req.headers before sending them. For example:

headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'X-Accept' : 'application/json'}

or:

req.headers['X-Accept'] = 'application/json'

Have a look at https://nodejs.org/api/http.html#http_http_request_options_callback .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM