Python:如果我的数据中有标点符号,请求将不会 POST

Python: Requests won't POST if I have punctuation in my data

我有一个 small_file.txt 文件,其中包含:

1asdfaksdjfhlaksjdhflkjashdflkjhasldkjfhlaksdfhasdf:
2asdfaksdjfhlaksjdhflkjashdflkjhasldkjfhlaksdfhasdf:
3asdfaksdjfhlaksjdhflkjashdflkjhasldkjfhlaksdfhasdf
4asdfaksdjfhlaksjdhflkjashdflkjhasldkjfhlaksdfhasdf:

注意末尾的冒号,它们只是常规字符串。

当我尝试使用 python requests 发送它时,它不起作用。出于某种原因,它等待带有冒号的第一行,然后发送从那里开始的所有行。因此,例如,在上面的文件中,它只会 POST

3asdfaksdjfhlaksjdhflkjashdflkjhasldkjfhlaksdfhasdf
4asdfaksdjfhlaksjdhflkjashdflkjhasldkjfhlaksdfhasdf:

我该如何解决这个问题?我不确定发生了什么。

这是我的代码的一个简单版本:

import requests
import sys
import json
import os


token                    = 'nVQowAng0c'
url                      = "https://api.hipchat.com/v2/room/test_room/share/file"
headers                  = {'Content-type': 'multipart/related; boundary=boundary123456'}
headers['Authorization'] = "Bearer " + token


filepath = 'small_file.csv'
data     = open(filepath, 'rb').read()

payload = """\
--boundary123456
Content-Type: application/json; charset=UTF-8
Content-Disposition: attachment; name="metadata"
--boundary123456
Content-Disposition: attachment; name="file"; filename="{0}"
{1}
--boundary123456--\
""".format(os.path.basename(filepath), data)


r = requests.post(url, headers=headers, data=payload)
r.raise_for_status()

当我尝试发送类似 .csv 文件的内容时,每行都有一个时间戳,但不会发送任何内容,因为每行都有一个冒号。

您的直接错误是错误编码了 MIME 多部分元素。每个部分有 两个 部分,headers 和内容,中间有一个双换行符。你的缺少第二个换行符,将其添加到:

payload = """\
--boundary123456
Content-Type: application/json; charset=UTF-8
Content-Disposition: attachment; name="metadata"

--boundary123456
Content-Disposition: attachment; name="file"; filename="{0}"

{1}
--boundary123456--\
""".format(os.path.basename(filepath), data)

我不会手动构建内容,但是 re-purpose requests-toolbelt project 让您以流方式上传数据:

from requests_toolbelt import MultipartEncoder


class MultipartRelatedEncoder(MultipartEncoder):
    """A multipart/related encoder"""
    @property
    def content_type(self):
        return str(
            'multipart/related; boundary={0}'.format(self.boundary_value)
        )

    def _iter_fields(self):
        # change content-disposition from form-data to attachment
        for field in super(MultipartRelatedEncoder, self)._iter_fields():
            content_type = field.headers['Content-Type']
            field.make_multipart(
                content_disposition='attachment', 
                content_type=content_type)
            yield field


m = MultipartRelatedEncoder(
    fields={
        'metadata': (None, '', 'application/json; charset=UTF-8'),
        'file': (os.path.basename(filepath), open(filepath, 'rb'), 'text/csv'),
    }
)

headers['Content-type'] = m.content_type

r = requests.post(url, data=m, headers=headers)

我已调整 requests_toolbelt.MultipartEncoder class 以发出 multipart/related 数据流而不是 multipart/form-data 消息。

请注意,我传入 打开文件 object,而不是文件数据本身;这是因为 MultipartEncoder 允许您 将数据流 到远程服务器,文件不必一次读入内存。

您可能想在 metadata 部分传入实际的 JSON 数据;用有效的 JSON 文档替换 (None, '', 'application/json; charset=UTF-8' 元组中的空字符串。

这是@Martijn Pieters 的组合代码:

# do this:
#     pip install requests_toolbelt

from os                import path
from sys               import exit, stderr
from requests          import post
from requests_toolbelt import MultipartEncoder


class MultipartRelatedEncoder(MultipartEncoder):
    """A multipart/related encoder"""
    @property
    def content_type(self):
        return str('multipart/related; boundary={0}'.format(self.boundary_value))

    def _iter_fields(self):
        # change content-disposition from form-data to attachment
        for field in super(MultipartRelatedEncoder, self)._iter_fields():
            content_type = field.headers['Content-Type']
            field.make_multipart(content_disposition = 'attachment',
                                 content_type        = content_type)
            yield field




def hipchat_file(token, room, filepath, host='api.hipchat.com'):

    if not path.isfile(filepath):
        raise ValueError("File '{0}' does not exist".format(filepath))


    url                      = "https://{0}/v2/room/{1}/share/file".format(host, room)
    headers                  = {'Content-type': 'multipart/related; boundary=boundary123456'}
    headers['Authorization'] = "Bearer " + token



    m = MultipartRelatedEncoder(fields={'metadata' : (None, '', 'application/json; charset=UTF-8'),
                                        'file'     : (path.basename(filepath), open(filepath, 'rb'), 'text/csv')})

    headers['Content-type'] = m.content_type

    r = post(url, data=m, headers=headers)

if __name__ == '__main__:

    my_token = <my token>   
    my_room  = <room name>    
    my_file  = <filepath>

    try:
        hipchat_file(my_token, my_room, my_file)
    except Exception as e:
        msg = "[ERROR] HipChat file failed: '{0}'".format(e)
        print(msg, file=stderr)
        exit(1)