2

I am trying to write an automatic subtitle finder in Python 3.x using The SubDB (http://thesubdb.com/api/). I'm now working on an upload feature. However, I cannot get it to work. I keep getting a 415 error (Unsupported Media Type). On the SubDB website, a 'request sample' is given:

POST /?action=upload HTTP/1.1
Host: api.thesubdb.com
User-Agent: SubDB/1.0 (Pyrrot/0.1; http://github.com/jrhames/pyrrot-cli)
Content-Length: 60047
Content-Type: multipart/form-data; boundary=xYzZY

- - --xYzZY
Content-Disposition: form-data; name="hash"

edc1981d6459c6111fe36205b4aff6c2
- - --xYzZY
Content-Disposition: form-data; name="file"; filename="subtitle.srt"
Content-Type: application/octet-stream


[PAYLOAD]

But I do not know how to interpret this and I couldn't find an answer online. This is my current code:

def uploadSubtitle(hash, path):
    params = {'action': "upload", 'hash': hash}
    response = requests.post(
        url=base_url.format(urllib.parse.urlencode(params)),
        data=open(path,'r').read(),
        headers = {
            'User-Agent': user_agent,
            'Content-Length': 51200,
            'Content-Type': "multipart/form-data; boundary=xYzZY",
            'Host': "api.thesubdb.com"
        }
    )
    return response.status_code

Any advice would be greatly appreciated!

Roberto
  • 958
  • 13
  • 33
  • I found a similar questions that might help http://stackoverflow.com/questions/12385179 – osowskit Jun 28 '15 at 15:19
  • I tried the following, but it still gives me 415... `response = requests.post( url=base_url.format(urllib.parse.urlencode(params)), files={'%s.srt' % hash: open(path, 'rb')}, headers = { 'User-Agent': user_agent, } )` – Roberto Jun 29 '15 at 07:33
  • Mee too :(, I have same problem!!! – Ivandro Jao Oct 21 '16 at 19:17

1 Answers1

-1

I was having the same issues.

You may want to see this https://github.com/ivandrofly/SubDBSharp

Try following:

    POST /?action=upload HTTP/1.1
    Host: api.thesubdb.com
    User-Agent: SubDB/1.0 (Pyrrot/0.1; http://github.com/jrhames/pyrrot-cli)
    Content-Length: [Subtitle content length including boundary length]
    Content-Type: multipart/form-data; boundary=xYzZY

    --xYzZY
    Content-Disposition: form-data; name="hash"

    edc1981d6459c6111fe36205b4aff6c2
    --xYzZY
    Content-Disposition: form-data; name="file"; filename="subtitle.srt"
    Content-Type: application/octet-stream

    [SUBTITLE CONTENT]
    --xYzZY


    # Content-Length:
    Content length should be from --xYzZY to last --xYzZY see above.

Note: Boundary started --xYzZY and ends after [SUBTITLE CONTENT]

Ivandro Jao
  • 2,731
  • 5
  • 24
  • 23