smirgol / plugin.video.crunchyroll

Watch videos from the anime platform Crunchyroll.com on Kodi
GNU Affero General Public License v3.0
45 stars 11 forks source link

fix: Playheads update failure because of bad request content type #30

Closed lumiru closed 6 months ago

lumiru commented 6 months ago

Content-Type in API is application/x-www-form-urlencoded. This value overrides the Content-Type uses to update playheads.

smirgol commented 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.

lumiru commented 6 months ago

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:

smirgol commented 6 months ago

Oh, that totally makes sense! :D Thank you for explaining.