莫愁前路无知己,天下谁人不识君。。。。

python接口测试---http请求之post(json格式)

上一篇 / 下一篇  2018-05-15 17:16:49 / 个人分类:测试技巧

python 发送HTTP请求 post json 格式:
# coding:utf-8
import json
import httplib
test_data = {'callSystem': '1', 'vehicleCode': '粤B44448'}
requrl = "http://10.203.26.18:8888/shivaResourceBase/temporaryVehicleService/canAssignAble"
headerdata = {"Content-type": "application/json"}
conn = httplib.HTTPConnection("10.203.26.18:8888")
conn.request('POST', requrl, json.dumps(test_data), headerdata)
response = conn.getresponse()
res = response.read()
print res
成功打印出JSON格式的数据,successful!
方法二:
import urllib2
import json
url = 'http://10.203.26.18:8888/shivaResourceBase/temporaryVehicleService/canAssignAble'
data = {'callSystem': '1', 'vehicleCode': '粤B44448'}
headers = {'Content-Type': 'application/json'}
request = urllib2.Request(url=url, headers=headers, data=json.dumps(data))
response = urllib2.urlopen(request)
print response.read()
定义函数:
def http_post():
    url = "http://10.203.26.18:8888/shivaResourceBase/temporaryVehicleService/canAssignAble"
    postdata = {'callSystem': '1', 'vehicleCode': '粤B44448'}
    req = urllib2.Request(url, json.dumps(postdata)) # 需要是json格式的参数
    req.add_header('Content-Type', 'application/json') # 要非常注意这行代码的写法
    response = urllib2.urlopen(req)
    # result = json.loads(response.read())  # 打印的中文未转码
    result = response.read()
    print result
http_post()



TAG: http---post

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar