python requests 的所有http请求体类型对应代码整理

261 阅读1分钟

header 是字典格式哦~

1.不发送请求体:

requests.request(method.upper(), url, headers=header, data = {} )

2.form-data

requests.request(method.upper(), url, headers=header, data=payload, files=files)

3.x-www-form-urlencoded

header['Content-Type'] = 'application/x-www-form-urlencoded'
payload = 'aaa=111&bbb=ccc'
requests.request(method.upper(), url, headers=header, data=payload)

4.raw
TEXT:

  header['Content-Type'] = 'text/plain'

JavaScript

header['Content-Type'] = 'application/javascript'

JSON

header['Content-Type'] = 'application/json'

HTML

 header['Content-Type'] = 'text/html'

XML

header['Content-Type'] = 'application/xml'

raw的请求代码都是一样,只是请求头的content-type不同

requests.request(method.upper(), url, headers=header, data=payload.encode('utf-8'))