我在将图像发送到休息服务时遇到问题。我知道服务正在运行,因为我已经将测试字符串("测试字符串")发送到服务,您可以在下面的shell信息中看到结果。我将图像转换为Base64然后尝试将其发送到服务并获得类型错误。我正在使用Python请求和base64库。下面显示了成功的呼叫和失败的呼叫。为了便于阅读,我缩短了base64字符串。
感谢您的帮助......
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Gpio pin 25 is HIGH
file name = /home/pi/photo/2016-01-06_17.59.31.jpg
{'Content-Type': 'application/json', 'Content-Length': '141',
'User-Agent': 'python-requests/2.4.3 CPython/3.4.2 Linux/4.1.13+',
'Accept': 'text/plain', 'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive'}
"Success"
https://alerts4yousvc.azurewebsites.net/api/SendReading
200
Gpio pin 25 is LOW
>>> ================================ RESTART ================================
>>>
Gpio pin 25 is HIGH
file name = /home/pi/photo/2016-01-06_18.00.32.jpg
Traceback (most recent call last):
File "/home/pi/Source/motionpi_http5.py", line 45, in <module>
r = requests.post("https://alerts4yousvc.azurewebsites.net/api/SendReading",
json=payload, headers=headers)
File "/usr/lib/python3/dist-packages/requests/api.py", line 94, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 49, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line
443, in request
prep = self.prepare_request(req)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line
374, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/usr/lib/python3/dist-packages/requests/models.py", line 307, in prepare
self.prepare_body(data, files, json)
File "/usr/lib/python3/dist-packages/requests/models.py", line 424,
in prepare_body
body = json_dumps(json)
File "/usr/lib/python3.4/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.4/json/encoder.py", line 192, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.4/json/encoder.py", line 250, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.4/json/encoder.py", line 173, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b'/9j/4WQGRXhpZgAATU0AKgAAAAgACgEAAAQAAAABAAABQAEBAAQAAAABAAAA8AEPAAIAAAAMAAAAhgEQAAIAAAAKAAAAkgEaAAUAAAABAAAAnAEbAAUAAAABAAAApAEoAAMAAAABAAIAAAEyAAIAAAAUAAAArAITAAMAAAABAAEAAIdpAAQAAAABAAAAwAAAA4hSYXNwYmVycnlQaQBSUF9PVjU2NDcAAAAASAAAAAEAAABIAAAAATIwMTY6MDE6MDYgMTg6MDA6MzIAABeCmgAFAAAAAQAAAdqCnQAFAAAAAQAAAeKIIgADAAAAAQADAACIJwADAAAAAQD6AACQAAAHAAAABDAyMjCQAwACAAAAFAAAAeqQBAACAAAAFAAAAf6RAQAHAAAABAECAwCSAQAKAAAAAQAAAhKSAgAFAAAAAQAAAhqSAwAKAAAAAQAAAiKSBQAFAAAAAQAAAiqSBwADAAAAAQACAACSCQADAAAAAQAAAACSCgAFAAAAAQAAAjKSfAAHAAABPAAAAjqgAAAHAAAABDAxMDCgAQADAAAAAQABAACgAgAEAAAAAQAAAUCgAwAEAAAAAQAAAPCgBQAEAAAAAQAAA3akAgADAAAAAQAAAACkAwADAAAAAQAAAAAAAAAAAAHoPgAPQkAAAHE4AAAnEDIwMTY6MDE6MDYgMTg6MDA6MzIAMjAxNjowMTowNiAxODowMDozMgAALcczAA9CQAAAd/EAACcQAAAAAwAAAGQAAHfxAAAnEAAAjIgAACcQZXY9LTEgbWx1IyxD/AP/Z'
is not JSON serializable
这是我正在使用的代码...
import RPi.GPIO as GPIO
import time
import picamera
import datetime
import base64
import requests
import urllib
import json
def get_file_name():
return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.jpg")
sensor = 25
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor,GPIO.IN,GPIO.PUD_DOWN)
previous_state = True
current_state = False
client_key="79c538c0f239"
task_key="ece0e81a26f1"
cam = picamera.PiCamera()
cam.resolution =(320,240)
while True:
time.sleep(1)
previous_state = current_state
current_state = GPIO.input(sensor)
# print("previous_state %s" %(previous_state))
# print("current_state %s" %(current_state))
if current_state != previous_state:
# print("4")
new_state = "HIGH" if current_state else "LOW"
print("Gpio pin %s is %s" % (sensor, new_state))
if current_state:
fileName = get_file_name()
imgpath = "/home/pi/photo/" + fileName
print("file name = %s" % (imgpath))
cam.capture(imgpath)
image_64 = base64.b64encode(open(imgpath,"rb").read())
payload = {"ClientKey": client_key,"TaskId": task_key, "MsgBody":image_64, "Reading":1}
headers = {'Content-Type': 'application/json', 'Accept':'text/plain'}
r = requests.post("https://alerts4yousvc.azurewebsites.net/api/SendReading", json=payload, headers=headers)
print(r.request.headers)
print(r.text)
print(r.url)
print(r.status_code)
答案 0 :(得分:3)
我想你可以做到
image_64 = str(base64.b64encode(open(imgpath,"rb").read()).decode("ascii"))