Closed lumiru closed 6 months ago
This one is a really strange one that's bugging me for the last days. It initially worked perfectly, but then it started to sometimes, for whatever reason, throw the invalid Content-Type error. And then it suddenly stops and works again, without any change.
When we send the json data, the content-type should be correctly set to application/json
- either by forcing it using
headers={
'Content-Type': 'application/json'
}
In the update request in videoplayer::thread_update_playhead()
or by ommitting it - the requests api should figure out itself, that it's an application/json
if I tell it to send json data.
It just makes no sense to me that the remote api sometimes errors out with an Invalid Content-Type
Now, what you do here is to merge the custom headers with the default ones, but that is done by the requests library anyway, when internally preparing the request, so it should not change anything.
It comes from the session management mechanism.
Python requests API uses application/json
as Content-Type only if no Content-Type is passed to its headers parameter. In fact, the way you merge headers parameters and API::api_headers
without this change implies that, if Content-Type exists in api_headers
, it will be the one the winner.
I only invert the way we merge them, so the forced value in videoplayer::thread_update_playhead
will always win after this fix.
To explain why the bug does not always occur, you have to look at how api_headers is set:
API::__init__
is called, API::api_headers
is with application/x-www-form-urlencoded
.API::start
is called, API::api_headers
is updated with an additional Authorization header (Content-Type is kept if exists)API::create_session
is called, API::api_headers
is cleared before Authorization
header is added (so Content-Type and User-Agent set as default are forgotten for the next requests)Oh, that totally makes sense! :D Thank you for explaining.
Content-Type in API is application/x-www-form-urlencoded. This value overrides the Content-Type uses to update playheads.