python中的Json和请求

2024-06-26 14:59:55 发布

您现在位置:Python中文网/ 问答频道 /正文

import requests
import json

url = 'mywebsite/test.php'
myobj = data = {"username" : "test", "password" : "1234"}


myobj = json.dumps(myobj)
x = requests.post("loginUser",url, data = myobj)

print(x)

我得到以下错误:

Traceback (most recent call last):
File "main.py", line 55, in <module> x = requests.post("loginUser",url, data = myobj) TypeError: post() got multiple values for argument 'data'

有人能帮忙吗


Tags: testimportjsonurldatausernamepasswordpost
3条回答

我自己正在学习python,并使用了一些请求

我总是将url放在最后,将有效负载放在最后:
i、 e "LoginUser"应该在最后走吗

看看这些文件:

https://docs.python-requests.org/en/latest/api/

requests.post(url, data=None, json=None, **kwargs)[source]
Sends a POST request.

Parameters: 
url – URL for the new Request object.
data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.
json – (optional) json data to send in the body of the Request.
**kwargs – Optional arguments that request takes.
Returns:    
Response object

因此,您的命令应该是:

myobj = json.dumps(myobj).encode("ascii")
x = requests.post(url = url, data = myobj)

或者不使用json.dumps:

x = requests.post(url = url, json = myobj)

在这种情况下,“loginUser”的具体用途是什么?这是URI路由、字段还是参数

我已经修好了。问题是我没有将http://放在url前面,也没有按照php所期望的方式格式化JSON请求

相关问题 更多 >