I have a small_file.txt
file that contains:
1asdfaksdjfhlaksjdhflkjashdflkjhasldkjfhlaksdfhasdf:
2asdfaksdjfhlaksjdhflkjashdflkjhasldkjfhlaksdfhasdf:
3asdfaksdjfhlaksjdhflkjashdflkjhasldkjfhlaksdfhasdf
4asdfaksdjfhlaksjdhflkjashdflkjhasldkjfhlaksdfhasdf:
Notice the colons at the end, they are just regular strings.
When I try to send it using python requests
it doesn't work. For some reason, it waits for the first line with a colon and then sends all the lines starting from there. So for example, in the file above, it will POST
only:
3asdfaksdjfhlaksjdhflkjashdflkjhasldkjfhlaksdfhasdf
4asdfaksdjfhlaksjdhflkjashdflkjhasldkjfhlaksdfhasdf:
How can I fix this issue? I'm not sure what is going on.
Here is a simple version of my code:
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()
When I try to send something like a .csv
file with a timestamp on every row, nothing will get sent because each row has a colon.