Locust: Post with JSON data

Created on 15 May 2014  ·  5Comments  ·  Source: locustio/locust

Can someone point me in the right direction on how to leverage locust to make a Post to an API endpoint that is expecting JSON data?

Most helpful comment

was getting HTTP415 until i read that last link

payload = {'some':'payload'}
headers = {'content-type': 'application/json'}
r = l.client.post("/post/endpoint", data=json.dumps(payload), headers=headers, catch_response=True)

specifying content type may be optional, if you don't care about the response.

All 5 comments

client is mostly just a requests object
Basic post example:
http://docs.locust.io/en/latest/quickstart.html?highlight=post

Extended post use (including mentioning data, which is what you want for JSON as dict):
http://docs.locust.io/en/latest/api.html#locust.clients.HttpSession.post

Requests documentation showing post of json examples:
http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests

was getting HTTP415 until i read that last link

payload = {'some':'payload'}
headers = {'content-type': 'application/json'}
r = l.client.post("/post/endpoint", data=json.dumps(payload), headers=headers, catch_response=True)

specifying content type may be optional, if you don't care about the response.

Shorter solution is

payload = {"some": "payload"}
r = l.client.post("/post/endpoint", json=payload)

This will encode JSON and set headers for you.

doesn't work. only HTTP 500 errors show up.

@cyberw When post with json and add headers, always 500....

Was this page helpful?
0 / 5 - 0 ratings