application/json 这个 Content-Type 作为响应头大家肯定不陌生。实际上,现在越来越多的人把它作为请求头,用来告诉服务端消息主体是序列化后的 JSON 字符串。由于 JSON 规范的流行,除了低版本 IE 之外的各大浏览器都原生支持 JSON.stringify,服务端语言也都有处理 JSON 的函数,使用 JSON 不会遇上什么麻烦。 圆柱模板
import requests import json CONFIG = { 'url': 'http://192.168.90.10:8888/', 'headers': {'Content-Type': 'application/json'} } data = {'content': 'hello', 'digital': '0', 'punctuate': '1', 'engModel': '2'} url = CONFIG['url'] headers = CONFIG['headers'] response = requests.post(url=url, data=json.dumps(data),headers=headers,timeout=1) print(response.content)