-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathpost_file.py
43 lines (28 loc) · 842 Bytes
/
post_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
# SOURCE: http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file
import requests
url = "https://httpbin.org/post"
abs_file_name = __file__
files = {"file": open(abs_file_name, "rb")}
rs = requests.post(url, files=files)
print(rs)
print(rs.text)
print()
# With file name
files = {"file": ("my_file.py", open(abs_file_name, "rb"))}
rs = requests.post(url, files=files)
print(rs)
print(rs.text)
print()
# Send string as file
files = {"file": ("report.csv", "some,data,to,send\nanother,row,to,send\n")}
rs = requests.post(url, files=files)
print(rs)
print(rs.text)
# Send bytes as file
files = {"file": ("report.csv", b"some,data,to,send\nanother,row,to,send\n")}
rs = requests.post(url, files=files)
print(rs)
print(rs.text)