NAME

HTTP::Promise::Headers::Accept - Accept Header Field

SYNOPSIS

use HTTP::Promise::Headers::Accept;
my $ac = HTTP::Promise::Headers::Accept->new || 
    die( HTTP::Promise::Headers::Accept->error, "\n" );
my $ac = HTTP::Promise::Headers::Accept->new( 'text/html, application/json, application/xml;q=0.9, */*;q=0.8' ) || 
    die( HTTP::Promise::Headers::Accept->error, "\n" );
$ac->add( 'text/html' );
$ac->add( 'application/json' => 0.7 );
$h->accept( $ac->as_string ); Accept: text/html, application/json;q=0.7
# or
$h->accept( "$ac" );
my $qv_elements = $ac->elements;
my $obj = $ac->get( 'text/html' );
# change the weight
$obj->value( 0.3 );
$ac->remove( 'text/html' );
my $sorted_objects = $ac->sort;
my $asc_sorted = $ac->sort(1);
# Returns a Module::Generic::Array object
my $ok = $ac->match( [qw( application/json text/html )] );

VERSION

v0.1.0

DESCRIPTION

The following description is taken from Mozilla documentation.

Accept: image/*
Accept: text/html
Accept: */*
Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8

See rfc7231 section 5.3.2 and rfc7231, section 5.3.5

METHODS

add

Provided with a mime type and a quality weight (q-value), and this will push a new HTTP::Promise::Field::QualityValue object to the stack of elements.

as_string

Returns a string representation of the Accept object.

elements

Retrieve an array object of HTTP::Promise::Field::QualityValue objects.

get

Provided with a mime type and this returns its HTTP::Promise::Field::QualityValue object entry, if any. If nothing corresponding was found, it returns an empty string (false, but not undef)

match

Provided with a either an array reference of proposed values, or a string, or something that stringifies, and this will return an array object of values that match the supported one and in the order of preference.

For example, consider:

Accept: text/html;q=0.3, application/json;q=0.7; text/plain;q=0.2

# We prefer text/html first and application/json second
my $ok = $ac->match( [qw( text/html application/json )] );

$ok would contain 2 entries: application/json and text/html

# Only take the first one
my $mime_type = $ac->match( [qw( text/html application/json )] )->first;
# or get it as a list
my @ok_types = $ac->match( [qw( text/html application/json )] )->list;

remove

Provided with a mime type and this removes its HTTP::Promise::Field::QualityValue object from the list of elements.

sort

This returns a sorted array object of HTTP::Promise::Field::QualityValue objects, based on their weight factor. If the option reverse is provided and true, this will sort the elements in reverse order, that is in incremental order, from smallest to biggest quality factor.

For example, the following:

text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8

will be sorted as:

text/html, application/xhtml+xml, image/webp, application/xml;q=0.9, */*;q=0.8

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

HTTP::Promise, HTTP::Promise::Request, HTTP::Promise::Response, HTTP::Promise::Message, HTTP::Promise::Entity, HTTP::Promise::Headers, HTTP::Promise::Body, HTTP::Promise::Body::Form, HTTP::Promise::Body::Form::Data, HTTP::Promise::Body::Form::Field, HTTP::Promise::Status, HTTP::Promise::MIME, HTTP::Promise::Parser, HTTP::Promise::IO, HTTP::Promise::Stream, HTTP::Promise::Exception

HTTP::Accept

COPYRIGHT & LICENSE

Copyright(c) 2022 DEGUEST Pte. Ltd.

All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.