Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Artax Laravel Package

amphp/artax

Deprecated (unmaintained) async HTTP/1.1 client for PHP built on Amp. Implements HTTP over raw TCP sockets (no ext/curl), with keep-alive pooling, redirects, gzip decoding, streaming bodies, TLS, cookies, and proxy support. Use amphp/http-client instead.

View on GitHub
Deep Wiki
Context7

title: Requests permalink: /requests

Artax allows either passing a string (which is interpreted as URI) or a Request object to Client::request(). The Request class can be used to specify further specifics of the request such as setting headers or changing the request method.

Request objects are immutable, all mutators return new instances.

Request URI

The constructor requires an absolute request URI. Request::withUri(string $uri) allows changing the request method.

$request = (new Request("https://httpbin.org/post", "POST"))
    ->withBody("foobar");
    
$request = $request->withUri("https://google.com/");

Request::getUri() exposes the request URI of the given Request object.

Request Method

The constructor accepts an optional request method, it defaults to GET. Request::withMethod(string $method) allows changing the request method.

$request = (new Request("https://httpbin.org/post", "POST"))
    ->withBody("foobar");
    
$request = $request->withMethod("PUT");

Request::getMethod() exposes the request method of the given Request object.

Request Headers

Request::withHeader(string $field, string $value) allows changing the request headers. It will remove any previous values for that field. Request::withAddedHeader(string $field, string $value) allows adding an additional header line without removing existing lines.

Request::withHeaders(array $headers) allows adding multiple headers at once with the array keys being the field names and the values being the header values. The header values can also be arrays of strings to set multiple header lines.

Request::hasHeader(string $field) checks whether at least one header line with the given name exists.

Request::getHeader(string $field) returns the first header line with the given name or null if no such header exists.

Request::getHeaderArray(string $field) returns an array of header lines with the given name. An empty array is returned if no header with the given name exists.

Request::getHeaders() returns an associative array with the keys being the header names and the values being arrays of header lines.

$request = (new Request("https://httpbin.org/post", "POST"))
    ->withHeader("X-Foobar", "Hello World")
    ->withBody("foobar");

Request Bodies

Request::withBody($body) allows changing the request body. Accepted types are string, null, and RequestBody. string and null are automatically converted to an instance of RequestBody.

{:.note}

RequestBody is basically a factory for request bodies. We cannot simply accept streams there, because a request body might have to be sent again on a redirect. Additionally, RequestBody allows the body to set headers, which can be used to automatically set headers such as Content-Type: application/json for a JsonBody. Note that headers set via RequestBody::getHeaders() are only applied if the Request doesn't have such a header. This allows overriding the default body header in a request.

$request = (new Request("https://httpbin.org/post", "POST"))
    ->withBody("foobar");

Request::getBody() exposes the request body of the given Request object and will always return a RequestBody.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity