简体   繁体   English

如何使用请求发布发送 BytesIO

[英]How to send BytesIO using requests post

I am trying to send msgpack encoded values to server using requests post to an insert rest endpoint which takes the serialized byte stream as input.我正在尝试使用发送到插入 rest 端点的请求将 msgpack 编码值发送到服务器,该端点将序列化字节 stream 作为输入。 But it seems like the values are not getting there properly as I see no values getting inserted in the table.但似乎值没有正确到达那里,因为我没有看到表中插入任何值。 I have never tried this before, so please pardon my ignorance.我以前从未尝试过,所以请原谅我的无知。 Here's what I am doing:这就是我正在做的事情:

buf = io.BytesIO()
for rows in dataframe:
    buf.write(msgpack.packb(rows))
    response = requests.post(url, data=buf, verify=False)
    try:
        response.raise_for_status()
    except requests.exceptions.HTTPError as err:
        print('Error fetching response using requests')

extending your answer, using a with block is advised in-order to fix memory leak in case of exception.扩展您的答案,建议使用 with 块,以便在出现异常时修复 memory 泄漏。

for rows in dataframe:
    with io.BytesIO(msgpack.packb(rows)) as buf:
        buf.seek(0)
        response = requests.post(url, data=buf, verify=False)
    try:
        response.raise_for_status()
    except requests.exceptions.HTTPError as err:
        print('Error fetching response using requests')

You can also add initial data when you create the BytesIO object so you don't have to call seek :您还可以在创建BytesIO object 时添加初始数据,因此您不必调用seek

>>> import io
>>> buf = io.BytesIO(b"test")
>>> buf.tell()
0
>>> buf.read()
b'test'
>>> buf.tell()
4

I am trying to send msgpack encoded values to server using requests post to an insert rest endpoint which takes the serialized byte stream as input.我正在尝试使用发送到插入 rest 端点的请求将 msgpack 编码值发送到服务器,该端点将序列化字节 stream 作为输入。 But it seems like the values are not getting there properly as I see no values getting inserted in the table.但似乎值没有正确到达那里,因为我没有看到表中插入任何值。 I have never tried this before, so please pardon my ignorance.我以前从未尝试过,所以请原谅我的无知。 Here's what I am doing:这就是我正在做的事情:

buf = io.BytesIO()
for rows in dataframe:
    buf.write(msgpack.packb(rows))
    response = requests.post(url, data=buf, verify=False)
    try:
        response.raise_for_status()
    except requests.exceptions.HTTPError as err:
        print('Error fetching response using requests')

Reading other similar kind of questions/answers, I even tried passing buf.seek(0) and buf.getvalue() .阅读其他类似的问题/答案,我什至尝试传递buf.seek(0)buf.getvalue() Not sure if that's appropriate to assign it to the data argument, but those didn't work either.不确定将其分配给 data 参数是否合适,但这些也不起作用。

Whenever I write to the buffer, the pointer will always point to the end of the buffer and waiting for a new write.每当我写入缓冲区时,指针将始终指向缓冲区的末尾并等待新的写入。 So, when I pass data=buf , that is going to make an insertion with no values.因此,当我通过data=buf时,将进行没有值的插入。 The solution is to do buf.seek(0) and then pass data=buf as a parameter or simply data=buf.seek(0)解决方案是做buf.seek(0)然后传递data=buf作为参数或简单地data=buf.seek(0)

So, the following works:因此,以下工作:

for rows in dataframe:
    buf = io.BytesIO()
    buf.write(msgpack.packb(rows))
    buf.seek(0)
    response = requests.post(url, data=buf, verify=False)
    try:
        response.raise_for_status()
    except requests.exceptions.HTTPError as err:
        print('Error fetching response using requests')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM