1、请求参数有多个,F12查看请求体如下:
查看源代码:
------WebKitFormBoundaryHknGXm9VkhRUXZYC
Content-Disposition: form-data; name="custId"
IICON004
------WebKitFormBoundaryHknGXm9VkhRUXZYC
Content-Disposition: form-data; name="custName"
zljun8210@live.cn
------WebKitFormBoundaryHknGXm9VkhRUXZYC
Content-Disposition: form-data; name="workOrderId"
396215
------WebKitFormBoundaryHknGXm9VkhRUXZYC
Content-Disposition: form-data; name="comment"
Yes
------WebKitFormBoundaryHknGXm9VkhRUXZYC
Content-Disposition: form-data; name="file"
undefined
------WebKitFormBoundaryHknGXm9VkhRUXZYC--
原始请求上的Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryHOwJrytA0cCkCKXb
2、 在Postman上添加HTTP请求,配置如下:
3、 Python代码处理如下:
import requests
import urllib3
from requests_toolbelt import MultipartEncoder
def customerReply(custid, woid, comments):
url = 'https://xxx.xxx.xxx/xxxxxx/api/workorder/addcomment'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'}
data = MultipartEncoder({
'custId': '%s' % custid,
'custName': 'AutoReplyer',
'workOrderId': '%s' % woid,
'comment': '%s' % comments
})
headers['Content-Type'] = data.content_type
urllib3.disable_warnings()
resut = requests.post(url=url, headers=headers, data=data, verify=False)
if resut.status_code == 200:
# sys.stderr.write('customer replied !')
return True
else:
return False
if __name__ == '__main__':
customerReply('IICON004', 396215, 'customer reply test.')
注意:与普通的接口测试有区别的是,headers里面的Content-Type要取消,另外定义 data.content_type,请求后会自动处理。
如果还是用以下这种header,Post请求后会报405错误。
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Content-Type': 'text/plain;charset=UTF-8'}
以上。