user1584421
user1584421

Reputation: 3863

Sending json requests to flask server in pytest

I want to send a json with a POST request, to a specific endpoint to my server, in pytest.

This is what i normally feed the server with curl:

curl --header "Content-Type: application/json" --request POST --data '{"item":"ZycugK6yXdHgf8foQTUnKY.txt","method":"one"}' 0.0.0.0:5000/convert

And this is how i tried to recreate it in pytest:

   data = {"item": filename, "method": method}
   response = client.post('/convert', content_type='application/json', data=data)

Where the filename, and method parameters are received as inputs to the function (forwarded by a decorator).

However, i keep getting a 400 - Bad Request, which means that i am doing something wrong with my request.

Upvotes: 0

Views: 758

Answers (2)

Icebreaker454
Icebreaker454

Reputation: 1081

It depends on the client you are using. For requests, you could use simply

response = client.post("/convert", json=data)

Upvotes: 1

Zelter Ady
Zelter Ady

Reputation: 6348

I think this will work:

data = {"item": filename, "method": method}
response = client.post('/convert', content_type='application/json', data=json.dumps(data))

You need to serialize the data.

Upvotes: 2

Related Questions