4.5 haas506问题汇总-关于http的post中的body
最新案例教程点击下方链接跳转,CSDN已停止更新
点
击
跳
转
1.将post请求中的body内容设置为可变
- 修改后的程序
由于案例程序默认将post请求中的body内容写死了,若想动态设置body里面的值可以按照下面的来
import ujson
....
def post():
global htp
#一个动态的值
lbs=[120.7421707,31.2800098]
header = 'Content-Type: application/json\r\nAccept: application/json\r\n'
#第一种body写法
# body = '{"carno": "A0001","name": "furtherbike","longitude":"120.7421707","latitude":"31.2800098","status":"open","coordinateX":200,"coordinateY": 200}'
#第二种body写法
body = {"carno": "A0001","name": "furtherbike","longitude": lbs[0],"latitude":lbs[1],"status":"open","coordinateX":200,"coordinateY": 200}
#使用ujson将字典转化为字符串
body=ujson.dumps(body)
url = 'yourURL'
htp.set_header(header)
htp.set_data(body,len(body))
ret = htp.post(url)
if ret < 0:
print('http post return failed %d'%ret)
return None
responsebody = htp.get_response()
print('responsebody:',responsebody)
responsecode = htp.get_response_code()
print('get_response_code:',responsecode)
....