415 code using httr and RCurl, but not just curl

笑着哭i 提交于 2019-12-06 05:15:30

That particular Spotify endpoint is looking for Content-Type: application/x-www-form-urlencoded vs JSON as you have it. This should work (it did for me):

library(httr)

response <- POST('https://accounts.spotify.com/api/token',
                 accept_json(),
                 authenticate(Sys.getenv("SPOTIFY_CLIENT_ID"), Sys.getenv("SPOTIFY_CLIENT_SECRET")),
                 body=list(grant_type='client_credentials'),
                 encode='form',
                 verbose())

(You can remove the verbose() in production)

Note the encode='form' but also note that you can use authenticate() vs build your own basic auth header (I store all keys in env variables, hence the use of Sys.getenv().

Justin Kibler

A HTTP 415 response code usually indicates you are specifying a Content-Type header that the server doesn't support, or maybe you aren't specifying one at all.

415 Unsupported Media Type: The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.

The postForm example you have above is specifying Content-Type: multipart/form-data;. I can reproduce the HTTP 415 response with the following curl:

curl -v -X POST 'https://accounts.spotify.com/api/token' -H 'Content-Type: multipart/form-data'

If you change the content type to application/x-www-form-urlencoded it should work.

curl -v -X POST 'https://accounts.spotify.com/api/token' -H 'Content-Type: application/x-www-form-urlencoded'
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!