0
我做了執行HTTP POST具有它博德編碼爲JSON調用的API一個簡單的腳本:當調用自定義API蟒蛇thows錯誤httplib.CannotSendHeader例外
#!/usr/bin/python
# coding: utf-8
import sys
import httplib
from base64 import b64encode
from json import dumps
filename = sys.argv[1]
outfile = filename + ".txt"
with open(filename, 'rb') as f:
content = f.read()
data = b64encode(content)
jsonData = {
'param_1': 12,
'param_2': 12,
'filename': "45.jpg",
'file_data': data
}
jsonData = dumps(jsonData)
headers = {
"Content-Type": 'application/json',
"Accept": '*/*'
}
conn = httplib.HTTPConnection('localhost',8016)
conn.putheader("Content-Type",'application/json')
conn.sendheaders()
conn.request('POST', '/files', jsonData, headers)
response = conn.getresponse()
print response
但我得到以下錯誤:
Traceback (most recent call last):
File "./add_file.py", line 31, in <module>
conn.putheader("Content-Type",'application/json')
File "/usr/lib/python2.7/httplib.py", line 1026, in putheader
raise CannotSendHeader()
httplib.CannotSendHeader
而我不明白爲什麼。我試圖做我能想到的任何事情,但我失敗了。從您的代碼
conn.putheader("Content-Type",'application/json')
線: