上传到API时出错415

时间:2015-06-28 12:59:42

标签: python api upload http-status-code-415

我正在尝试使用SubDB(http://thesubdb.com/api/)在Python 3.x中编写自动字幕查找器。我现在正在开发上传功能。但是,我无法让它发挥作用。我一直收到415错误(不支持的媒体类型)。在SubDB网站上,提供了“请求样本”:

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]

但我不知道如何解释这个,我在网上找不到答案。这是我目前的代码:

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

非常感谢任何建议!

1 个答案:

答案 0 :(得分:-1)

我遇到了同样的问题。

您可能希望看到此https://github.com/ivandrofly/SubDBSharp

请尝试以下操作:

    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.

注意:边界开始--xYzZY并在[SUBTITLE CONTENT]之后结束