Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post with JSON data #157

Closed
BenjaminKang opened this issue May 15, 2014 · 6 comments
Closed

Post with JSON data #157

BenjaminKang opened this issue May 15, 2014 · 6 comments

Comments

@BenjaminKang
Copy link

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?

@daubman
Copy link
Contributor

daubman commented May 16, 2014

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

@ghost
Copy link

ghost commented Jun 26, 2014

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.

@Jahaja Jahaja closed this as completed Dec 6, 2014
@osyvokon
Copy link

Shorter solution is

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

This will encode JSON and set headers for you.

@SehgalDivij
Copy link

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

@MarcSteven
Copy link

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

@Alfex4936
Copy link

rather than using json.dumps, we can try to pass string as data parameter

MY_JSON = """
{
  "intent": {
    "id": "123",
    "name": "456"
  }
}
"""

class LoadTesting(HttpUser):
    @task
    def parse_last_notice(self):
        self.client.post("/somewhere", data=MY_JSON )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants