Locust: Issue with indendation on a PUT request (Newbie)

Created on 3 Aug 2017  Â·  3Comments  Â·  Source: locustio/locust

Hi All,

Really struggling, I need to make a simple PUT request to our API endpoint. I've written what I think would work? But honestly I have little to no python experience.

Can someone please help me out to get a working request? Attached is the error im currently getting... I'll paste my python file..
error.txt

from locust import HttpLocust, TaskSet, task, json

class loadParcel(TaskSet):
def on_start(self):
self.scanParcel()

@task
def scanParcel(self):
    headers = {'content-type': 'application/json' , 'dpdsession':'06784db0-784a-11e7-a1fa-85ad1b51d266', 'highlander':'true', 'cache-control':'no-cache', 'dpdclient':'application/json'}
    payload = {"driverCode":"0056*FD8608", "labelNumber":"%0ML11XR15500748427135832826", "dateTime":"2017-08-03 03:00:41" }
        self.client.put("/depot/0056/route/796/?action=loadParcel", data=json.dumps(payload) , headers=headers)

class WebsiteUser(HttpLocust):
task_set = loadParcel
min_wait = 1000
max_wait = 1000

Most helpful comment

@ashleigh-robinson In Python you must indent method definitions 4 spaces from the beginning of the namespace declaration, and statements must be indented 4 spaces from the declaration of the method block. Here is your file re-indented. You can install the Flake8 Python linter Sublime Text package to show you these errors and more.

~~~python
from locust import HttpLocust, TaskSet, json, task

class LoadParcel(TaskSet):
def on_start(self):
self.scanParcel()

@task(1)
def scan_parcel(self):
    headers = {'content-type': 'application/json', 'dpdsession': '06784db0-784a-11e7-a1fa-85ad1b51d266', 'highlander': 'true', 'cache-control': 'no-cache', 'dpdclient': 'application/json'}
    payload = {"driverCode": "0056*FD8608", "labelNumber": "%0ML11XR15500748427135832826", "dateTime": "2017-08-03 03:00:41"}

    self.client.put("/depot/0056/route/796/?action=loadParcel", data=json.dumps(payload), headers=headers)

class WebsiteUser(HttpLocust):
task_set = LoadParcel
min_wait = 1000
max_wait = 1000
~~~

All 3 comments

@ashleigh-robinson In Python you must indent method definitions 4 spaces from the beginning of the namespace declaration, and statements must be indented 4 spaces from the declaration of the method block. Here is your file re-indented. You can install the Flake8 Python linter Sublime Text package to show you these errors and more.

~~~python
from locust import HttpLocust, TaskSet, json, task

class LoadParcel(TaskSet):
def on_start(self):
self.scanParcel()

@task(1)
def scan_parcel(self):
    headers = {'content-type': 'application/json', 'dpdsession': '06784db0-784a-11e7-a1fa-85ad1b51d266', 'highlander': 'true', 'cache-control': 'no-cache', 'dpdclient': 'application/json'}
    payload = {"driverCode": "0056*FD8608", "labelNumber": "%0ML11XR15500748427135832826", "dateTime": "2017-08-03 03:00:41"}

    self.client.put("/depot/0056/route/796/?action=loadParcel", data=json.dumps(payload), headers=headers)

class WebsiteUser(HttpLocust):
task_set = LoadParcel
min_wait = 1000
max_wait = 1000
~~~

@bgroupe thank you so much for your help, I understand now, I ran the above, it failed as I wrongly named the definition, after altering this it now succeeds!

One last question for everyone, and I promise I'll leave you all alone, how do i log the request response body?

Thanks

Depends on what you mean by _log_ -- if you want to log your requests and responses for every call you can just import the Python logging module:

~~~python
import logging as Logger

class LoadParcel(TaskSet):
...
def scan_parcel(self):
...
response = self.client.put("/resource", headers=headers, json=payload)
Logger.info(response.request.headers)
Logger.info(response.request.body)
...

~~~

If you want to view the request body in the Locust web interface on failure, I would look at the response context manager built into Locust, which will output to the Failures tab.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

styk-tv picture styk-tv  Â·  3Comments

foxracle picture foxracle  Â·  3Comments

ghost picture ghost  Â·  3Comments

sanyco92 picture sanyco92  Â·  4Comments

ShaolongHu picture ShaolongHu  Â·  3Comments