Skip to main content

AsyncClient

An async version of Client that creates ASGIRequests and calls through an async request path.

Attributes

AttributeTypeDescription
handler[AsyncClientHandler](asyncclienthandler.md?sid=django_test_client_asyncclienthandler)The asynchronous request handler used to process ASGI scopes and return responses.
raise_request_exceptionboolean = TrueFlag determining whether to re-raise exceptions that occur during the request processing.
exc_infotuple or None = NoneStores exception information captured during the request lifecycle for later inspection.
extradict or None = NoneStores additional keyword arguments passed to request methods to be used in the request scope.
headersdict or None = NoneStores the HTTP headers to be included in the outgoing asynchronous requests.

Constructor

Signature

def AsyncClient(
enforce_csrf_checks: boolean = False,
raise_request_exception: boolean = True,
headers: dict = None,
query_params: dict = None,
defaults: dict
) - > null

Parameters

NameTypeDescription
enforce_csrf_checksboolean = FalseWhether to enforce CSRF validation on requests.
raise_request_exceptionboolean = TrueWhether to raise exceptions that occur during the request process.
headersdict = NoneDefault headers to include in every request.
query_paramsdict = NoneDefault query parameters to include in every request.
defaultsdictAdditional default keyword arguments for the request factory.

Methods


request()

@classmethod
def request(
**request: dict
) - > [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Make a generic request. Compose the scope dictionary and pass to the handler, return the result of the handler. Assume defaults for the query environment, which can be overridden using the arguments to the request.

Parameters

NameTypeDescription
**requestdictKey-value pairs used to construct the ASGI scope and override default request environment settings.

Returns

TypeDescription
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The response object populated with template data, context, and resolver match information.

get()

@classmethod
def get(
path: str,
data: dict = None,
follow: bool = False,
secure: bool = False,
headers: dict = None,
query_params: dict = None
) - > [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Request a response from the server using GET.

Parameters

NameTypeDescription
pathstrThe URL path to request from the server.
datadict = NoneDictionary of query parameters to be appended to the URL.
followbool = FalseWhether to automatically follow HTTP redirects until a non-3xx status is reached.
securebool = FalseIf True, the request will be performed over HTTPS.
headersdict = NoneAdditional HTTP headers to include in the request.
query_paramsdict = NoneExplicit query string parameters to include in the request scope.

Returns

TypeDescription
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The server response, potentially after following redirects if requested.

post()

@classmethod
def post(
path: str,
data: dict|str = None,
content_type: str = multipart/form-data,
follow: bool = False
) - > [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Request a response from the server using POST.

Parameters

NameTypeDescription
pathstrThe URL path where the data should be posted.
data`dictstr` = None
content_typestr = multipart/form-dataThe MIME type of the request body.
followbool = FalseWhether to follow redirects triggered by the POST request.

Returns

TypeDescription
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The server response resulting from the POST submission.

@classmethod
def head(
path: str
) - > [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Request a response from the server using HEAD.

Parameters

NameTypeDescription
pathstrThe URL path to query for header information.

Returns

TypeDescription
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The response headers from the server without the response body.

options()

@classmethod
def options(
path: str
) - > [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Request a response from the server using OPTIONS.

Parameters

NameTypeDescription
pathstrThe URL path to query for allowed methods and capabilities.

Returns

TypeDescription
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The response describing the communication options available for the target URL.

put()

@classmethod
def put(
path: str
) - > [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Send a resource to the server using PUT.

Parameters

NameTypeDescription
pathstrThe URL path of the resource to be created or replaced.

Returns

TypeDescription
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The response from the server after the resource update attempt.

patch()

@classmethod
def patch(
path: str
) - > [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Send a resource to the server using PATCH.

Parameters

NameTypeDescription
pathstrThe URL path of the resource to be partially modified.

Returns

TypeDescription
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The response from the server after applying the partial update.

delete()

@classmethod
def delete(
path: str
) - > [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Send a DELETE request to the server.

Parameters

NameTypeDescription
pathstrThe URL path of the resource to be removed.

Returns

TypeDescription
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The response from the server confirming or denying the deletion.

trace()

@classmethod
def trace(
path: str
) - > [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Send a TRACE request to the server.

Parameters

NameTypeDescription
pathstrThe URL path to perform a diagnostic trace against.

Returns

TypeDescription
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The response containing the loop-back message of the request entity.