Send an HTTP request :param method: method for the new :class:`Request` object. :param url: path that will be concatenated with the base host URL that has been specified. Can also be a full URL, in which case the full URL will be requested, and the base host
(
self,
method: str,
url: str,
name: str | None = None,
data: str | dict | None = None,
catch_response: bool = False,
stream: bool = False,
headers: dict | None = None,
auth: tuple[str | bytes, str | bytes] | None = None,
json: Any = None,
allow_redirects: bool = True,
context: dict = {},
**kwargs,
)
| 188 | return resp |
| 189 | |
| 190 | def request( |
| 191 | self, |
| 192 | method: str, |
| 193 | url: str, |
| 194 | name: str | None = None, |
| 195 | data: str | dict | None = None, |
| 196 | catch_response: bool = False, |
| 197 | stream: bool = False, |
| 198 | headers: dict | None = None, |
| 199 | auth: tuple[str | bytes, str | bytes] | None = None, |
| 200 | json: Any = None, |
| 201 | allow_redirects: bool = True, |
| 202 | context: dict = {}, |
| 203 | **kwargs, |
| 204 | ) -> ResponseContextManager: # technically it can also return FastResponse |
| 205 | """ |
| 206 | Send an HTTP request |
| 207 | |
| 208 | :param method: method for the new :class:`Request` object. |
| 209 | :param url: path that will be concatenated with the base host URL that has been specified. |
| 210 | Can also be a full URL, in which case the full URL will be requested, and the base host |
| 211 | is ignored. |
| 212 | :param name: (optional) An argument that can be specified to use as label in Locust's |
| 213 | statistics instead of the URL path. This can be used to group different URL's |
| 214 | that are requested into a single entry in Locust's statistics. |
| 215 | :param catch_response: (optional) Boolean argument that, if set, can be used to make a request |
| 216 | return a context manager to work as argument to a with statement. This will allow the |
| 217 | request to be marked as a fail based on the content of the response, even if the response |
| 218 | code is ok (2xx). The opposite also works, one can use catch_response to catch a request |
| 219 | and then mark it as successful even if the response code was not (i.e. 500 or 404). |
| 220 | :param data: (optional) String/bytes to send in the body of the request. |
| 221 | :param json: (optional) Json to send in the body of the request. |
| 222 | Automatically sets Content-Type and Accept headers to "application/json". |
| 223 | Only used if data is not set. |
| 224 | :param headers: (optional) Dictionary of HTTP Headers to send with the request. |
| 225 | :param auth: (optional) Auth (username, password) tuple to enable Basic HTTP Auth. |
| 226 | :param stream: (optional) If set to true the response body will not be consumed immediately |
| 227 | and can instead be consumed by accessing the stream attribute on the Response object. |
| 228 | Another side effect of setting stream to True is that the time for downloading the response |
| 229 | content will not be accounted for in the request time that is reported by Locust. |
| 230 | :param allow_redirects: (optional) Set to True by default. |
| 231 | :return: A :py:class:`FastResponse <locust.contrib.fasthttp.FastResponse>` object if catch_response is False, and |
| 232 | :py:class:`ResponseContextManager <locust.contrib.fasthttp.ResponseContextManager>` if True. |
| 233 | """ |
| 234 | # prepend url with hostname unless it's already an absolute URL |
| 235 | built_url = self._build_url(url) |
| 236 | |
| 237 | start_time = time.time() # seconds since epoch |
| 238 | |
| 239 | if self.user: |
| 240 | context = {**self.user.context(), **context} |
| 241 | |
| 242 | headers = headers or {} |
| 243 | if auth: |
| 244 | headers["Authorization"] = _construct_basic_auth_str(auth[0], auth[1]) |
| 245 | elif self.auth_header: |
| 246 | headers["Authorization"] = self.auth_header |
| 247 | if "Accept-Encoding" not in headers and "accept-encoding" not in headers: |