HTTP-Promise

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN


    Boolean. When true, this will have [HTTP::Promise](https://metacpan.org/pod/HTTP%3A%3APromise) HTTP methods return a [HTTP::Promise](https://metacpan.org/pod/promise), and when false, it returns directly the [HTTP::Promise::Response](https://meta...

# METHODS

The following methods are available. This interface provides similar interface as [LWP::UserAgent](https://metacpan.org/pod/LWP%3A%3AUserAgent) while providing more granular control.

## accept\_encoding

String. Sets or gets whether we should accept compressed data.

You can set it to `none` to disable it. By default, this is `auto`, and it will set the `Accept-Encoding` `HTTP` header to all the supported encoding based on the availability of associated modules.

You can also set this to a comma-separated list of known encoding, typically: `bzip2,deflate,gzip,rawdeflate,brotli`

See [HTTP::Promise::Stream](https://metacpan.org/pod/HTTP%3A%3APromise%3A%3AStream) for more details.

Returns a [scalar object](https://metacpan.org/pod/Module%3A%3AGeneric%3A%3AScalar) of the current value.

## accept\_language

An array of acceptable language. This will be used to set the `Accept-Language` header.

See also [HTTP::Promise::Headers::AcceptLanguage](https://metacpan.org/pod/HTTP%3A%3APromise%3A%3AHeaders%3A%3AAcceptLanguage)

## agent

This is a string.

Sets or gets the agent id used to identify when making the server connection.

It defaults to `HTTP-Promise/v0.1.0`

    my $p = HTTP::Promise->new( agent => 'MyBot/1.0' );
    $p->agent( 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0' );

The `User-Agent` header field is only set to this provided value if it is not already set.

## accept\_language

Sets or gets an array of acceptable response content languages.

For example:

    $http->accept_language( [qw( fr-FR ja-JP en-GB en )] );

Would result into an `Accept-Language` header set to `fr-FR;q=0.9,ja-JP;q=0.8,en-GB;q=0.7,en;q=0.6`

The `Accept-Language` header would only be set if it is not set already.

## auto\_switch\_https

Boolean. If set to a true value, or if left to `undef` (default value), this will set the `Upgrade-Insecure-Requests` header field to `1`

## buffer\_size

The size of the buffer to use when reading data from the filehandle or socket.

## connection\_header

Sets or gets the value for the header `Connection`. It can be `close` or `keep-alive`

If it is let `undef`, this module will try to guess the proper value based on the ["protocol" in HTTP::Promise::Request](https://metacpan.org/pod/HTTP%3A%3APromise%3A%3ARequest#protocol) and ["version" in HTTP::Promise::Request](https://metacpan.org/...

For protocol `HTTP/1.0`, `Connection` value would be `close`, but above `HTTP/1.1` the connection can be set to `keep-alive` and thus be re-used.

## cookie\_jar

Sets or gets the Cookie jar class object to use. This is typically [Cookie::Jar](https://metacpan.org/pod/Cookie%3A%3AJar) or maybe [HTTP::Cookies](https://metacpan.org/pod/HTTP%3A%3ACookies)

This defaults to [Cookie::Jar](https://metacpan.org/pod/Cookie%3A%3AJar)

    use Cookie::Jar;
    my $jar = Cookie::Jar->new;
    my $p = HTTP::Promise->new( cookie_jar => $jar );
    $p->cookie_jar( $jar );

## decodable

This calls ["decodable" in HTTP::Promise::Stream](https://metacpan.org/pod/HTTP%3A%3APromise%3A%3AStream#decodable) passing it whatever arguments that were provided.

## default\_header

Sets one more default headers. This is a shortcut to `$p->default_headers->header`

    $p->default_header( $field );
    $p->default_header( $field => $value );
    $p->default_header( 'Accept-Encoding' => scalar( HTTP::Promise->decodable ) );
    $p->default_header( 'Accept-Language' => 'fr, en, ja' );

## default\_headers

Sets or gets the [default header object](https://metacpan.org/pod/HTTP%3A%3APromise%3A%3AHeaders), which is set to `undef` by default.

This can be either an [HTTP::Promise::Headers](https://metacpan.org/pod/HTTP%3A%3APromise%3A%3AHeaders) or [HTTP::Headers](https://metacpan.org/pod/HTTP%3A%3AHeaders) object.

    use HTTP::Promise::Headers;
    my $headers = HTTP::Promise::Headers->new(
        'Accept-Encoding' => scalar( HTTP::Promise->decodable ),
        'Accept-Language' => 'fr, en, ja',
    );
    my $p = HTTP::Promise->new( default_headers => $headers );

## default\_protocol

Sets or gets the default protocol to use. For example: `HTTP/1.1`

## delete

Provided with an `uri` and an optional hash of header name/value pairs, and this will issue a `DELETE` http request to the given `uri`.

It returns a [promise](https://metacpan.org/pod/Promise%3A%3AMe), which can be used to call one or more [then](https://metacpan.org/pod/Promise%3A%3AMe#then) and [catch](https://metacpan.org/pod/Promise%3A%3AMe#catch)

    # or $p->delete( $uri, $field1 => $value1, $field2 => $value2 )
    $p->delete( $uri )->then(sub
    {
        my( $resolve, $reject ) = @$_;
        # an HTTP::Promise::Response is returned
        my $resp = shift( @_ );
        # Do something with the $resp object
    })->catch(sub
    {
        my $ex = shift( @_ );
        # An HTTP::Promise::Exception object is passed with an error code
        say( "Error code; ", $ex->code, " and message: ", $ex->message );



( run in 0.680 second using v1.01-cache-2.11-cpan-71847e10f99 )