jeffinter
jeffinter

Reputation: 7

Json and requests in 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)

I get the following error:

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'

Can anyone help with this?

Upvotes: 0

Views: 927

Answers (3)

jeffinter
jeffinter

Reputation: 7

I have fixed it. Problem was that I didn’t put http:// in front of the url and I didn’t format my JSON request the way my php was expecting.

Upvotes: 0

G.S
G.S

Reputation: 565

Look at the docs:

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

and so your command should be:

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

or without using json.dumps:

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

What exactly is "loginUser" for in this case? is that a URI route, field, or parameter?

Upvotes: 1

TwizzleBizzle
TwizzleBizzle

Reputation: 24

I'm just learning python myself and have used requests a bit.

I've always put the url at the beggining and payload at the end:
i.e Should "LoginUser" go at the end?

Upvotes: 0

Related Questions