python - JSON有效负载的Python字符串连接
问题描述
我正在尝试修改 Postman 生成的这段代码,以用字符串变量替换硬编码的字符串,但我不断收到
KeyError: '\n\t"username"'
这是代码
username = "jose"
email = "some_email"
password = "1234"
url = "some_url"
payload = '{\n\t\"username\": {},\n\t\"email\": {},\n\t\"password\": {}\n}'.format(username, email, password)
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text.encode('utf8'))
解决方案
看看这里的例子:https ://requests.readthedocs.io/en/master/user/quickstart/
>>> r = requests.post('https://httpbin.org/post', data = {'key':'value'})
您可以只对数据使用字典。
推荐阅读
- java - Eclipse 和 Processing 之间的窗口大小不同
- python - 如何在期间按天对 pandas 数据帧进行重新采样?
- python - 使用机器人向 Discord 频道发送消息
- python-3.x - 如何从 pandas 列中生成 X 个随机 3 组?
- react-native - 无法在我的反应项目中安装 Native Base
- .net - 如何更改 Visual Studio .net 安装路径
- selenium - 如何在 kotlin 中使用 div 角色和 aria-label 设置 xpath
- java - Android Webview 和 SupportMapFragment 使应用程序崩溃并出现错误:“E/chromium:[...] 支持的制服或变量太少。”
- python - Pip 安装到错误的文件夹
- matlab - MATLAB中积分函数的乘法误差