HTTP-Request-FromCurl
view release on metacpan or search on metacpan
lib/HTTP/Request/CurlParameters.pm view on Meta::CPAN
C<uri>
uri => 'https://example.com'
The URI of the request.
=cut
has uri => (
is => 'ro',
default => 'https://example.com',
);
=item *
C<headers>
headers => {
'Content-Type' => 'text/json',
'X-Secret' => ['value-1', 'value-2'],
}
The headers of the request. Multiple headers with the same
name can be passed as an arrayref to the header key.
=cut
has headers => (
is => 'ro',
default => sub { {} },
);
=item *
C<cookie_jar>
The cookie jar to use.
=cut
has cookie_jar => (
is => 'ro',
);
=item *
C<cookie_jar_options>
Options for the constructor of the cookie jar.
=cut
has cookie_jar_options => (
is => 'ro',
default => sub { {} },
);
=item *
C<credentials>
credentials => 'hunter2:secret'
The credentials to use for basic authentication.
=cut
has credentials => (
is => 'ro',
);
=item *
C<auth>
auth => 'basic'
The authentication method to use.
=cut
has auth => (
is => 'ro',
);
=item *
C<post_data>
post_data => ['A string','across multiple','scalars']
The POST body to use.
=cut
has post_data => (
is => 'ro',
default => sub { [] },
);
=item *
C<body>
body => '{"greeting":"Hello"}'
The body of the request.
=cut
has body => (
is => 'ro',
);
=item *
C<timeout>
timeout => 50
The timeout for the request
=cut
has timeout => (
is => 'ro',
);
lib/HTTP/Request/CurlParameters.pm view on Meta::CPAN
sub as_snippet( $self, %options ) {
my $type = delete $options{ type } || 'LWP';
if( 'LWP' eq $type ) {
$self->as_lwp_snippet( %options )
} elsif( 'Tiny' eq $type ) {
$self->as_http_tiny_snippet( %options )
} elsif( 'Mojolicious' eq $type ) {
$self->as_mojolicious_snippet( %options )
} else {
croak "Unknown type '$type'.";
}
}
sub as_lwp_snippet( $self, %options ) {
$options{ prefix } ||= '';
$options{ implicit_headers } ||= [];
my @preamble;
my @postamble;
my %ssl_options;
push @preamble, @{ $options{ preamble } } if $options{ preamble };
push @postamble, @{ $options{ postamble } } if $options{ postamble };
my @setup_ua = ('');
my $request_args = join ", ",
'$r',
$self->_pairlist([
maybe ':content_file', $self->output
], '')
;
my $init_cookie_jar = $self->_init_cookie_jar_lwp();
if( my $p = $init_cookie_jar->{preamble}) {
push @preamble, @{$p}
};
if( $self->insecure ) {
push @preamble, 'use IO::Socket::SSL;';
$ssl_options{ SSL_verify_mode } = \'IO::Socket::SSL::SSL_VERIFY_NONE';
$ssl_options{ SSL_hostname } = '';
$ssl_options{ verify_hostname } = '';
};
if( $self->cert ) {
push @preamble, 'use IO::Socket::SSL;';
$ssl_options{ SSL_ca_file } = $self->cert;
};
if( $self->capath ) {
push @preamble, 'use IO::Socket::SSL;';
$ssl_options{ SSL_ca_path } = $self->capath;
};
my $constructor_args = join ",",
$self->_pairlist([
send_te => 0,
maybe local_address => $self->local_address,
maybe max_size => $self->max_filesize,
maybe timeout => $self->timeout,
maybe cookie_jar => $init_cookie_jar->{code},
maybe SSL_opts => keys %ssl_options ? \%ssl_options : undef,
], '')
;
if( defined( my $credentials = $self->credentials )) {
my( $user, $pass ) = split /:/, $credentials, 2;
my $setup_credentials = sprintf qq{\$ua->credentials("%s","%s");},
quotemeta $user,
quotemeta $pass;
push @setup_ua, $setup_credentials;
};
if( $self->show_error ) {
push @postamble,
' die $res->message if $res->is_error;',
} elsif( $self->fail ) {
push @postamble,
' exit 1 if !$res->{success};',
};
@setup_ua = ()
if @setup_ua == 1;
my $request_constructor;
if( $self->method ne 'GET' and @{ $self->form_args }) {
push @preamble, 'use HTTP::Request::Common;';
push @{$options{ implicit_headers }}, 'Content-Type';
$request_constructor = <<SNIPPET;
my \$r = HTTP::Request::Common::@{[$self->method]}(
'@{[$self->uri]}',
Content_Type => 'form-data',
Content => [
@{[$self->_pairlist($self->form_args, ' ')]}
],
@{[$self->_build_lwp_headers(' ', %options)]}
);
SNIPPET
} else {
$request_constructor = <<SNIPPET;
my \$r = HTTP::Request->new(
'@{[$self->method]}' => '@{[$self->uri]}',
[
@{[$self->_build_lwp_headers(' ', %options)]}
],
@{[$self->_build_quoted_body()]}
);
SNIPPET
}
@preamble = map { "$options{prefix} $_\n" } @preamble;
@postamble = map { "$options{prefix} $_\n" } @postamble;
@setup_ua = map { "$options{prefix} $_\n" } @setup_ua;
return <<SNIPPET;
@preamble
my \$ua = LWP::UserAgent->new($constructor_args);@setup_ua
$request_constructor
my \$res = \$ua->request( $request_args );
@postamble
SNIPPET
};
sub as_http_tiny_snippet( $self, %options ) {
$options{ prefix } ||= '';
$options{ implicit_headers } ||= [];
push @{ $options{ implicit_headers }}, 'Host'; # HTTP::Tiny dislikes that header
my @preamble;
my @postamble;
my %ssl_options;
push @preamble, @{ $options{ preamble } } if $options{ preamble };
push @postamble, @{ $options{ postamble } } if $options{ postamble };
my @setup_ua = ('');
my $request_args = join ", ",
'$r',
$self->_pairlist([
maybe ':content_file', $self->output
], '')
;
my $init_cookie_jar = $self->_init_cookie_jar_tiny();
if( my $p = $init_cookie_jar->{preamble}) {
push @preamble, @{$p}
};
my @ssl;
if( $self->insecure ) {
} else {
push @ssl, verify_SSL => 1;
};
if( $self->cert ) {
push @preamble, 'use IO::Socket::SSL;';
$ssl_options{ SSL_cert_file } = $self->cert;
};
if( $self->show_error ) {
push @postamble,
' die $res->{reason} if !$res->{success};',
} elsif( $self->fail ) {
push @postamble,
' exit 1 if !$res->{success};',
};
my $constructor_args = join ",",
$self->_pairlist([
@ssl,
maybe timeout => $self->timeout,
maybe local_address => $self->local_address,
maybe max_size => $self->max_filesize,
maybe cookie_jar => $init_cookie_jar->{code},
maybe SSL_options => keys %ssl_options ? \%ssl_options : undef,
], '')
;
if( defined( my $credentials = $self->credentials )) {
my( $user, $pass ) = split /:/, $credentials, 2;
my $setup_credentials = sprintf qq{\$ua->credentials("%s","%s");},
quotemeta $user,
quotemeta $pass;
push @setup_ua, $setup_credentials;
};
@setup_ua = ()
if @setup_ua == 1;
@preamble = map { "$options{prefix} $_\n" } @preamble;
@postamble = map { "$options{prefix} $_\n" } @postamble;
@setup_ua = map { "$options{prefix} $_\n" } @setup_ua;
my @content = $self->_build_quoted_body();
if( grep {/\S/} @content ) {
unshift @content, 'content => ',
} elsif( @{ $self->form_args }) {
my $req = HTTP::Request::Common::POST(
'https://example.com',
Content_Type => 'form-data',
Content => $self->form_args,
);
@content = ('content => ', $self->_build_quoted_body( $req->content ));
$self->headers->{ 'Content-Type' } = join "; ", $req->headers->content_type;
}
return <<SNIPPET;
@preamble
my \$ua = HTTP::Tiny->new($constructor_args);@setup_ua
my \$res = \$ua->request(
'@{[$self->method]}' => '@{[$self->uri]}',
{
headers => {
@{[$self->_build_tiny_headers(' ', %options)]}
},
@content
},
);
@postamble
SNIPPET
};
sub as_mojolicious_snippet( $self, %options ) {
$options{ prefix } ||= '';
$options{ implicit_headers } ||= [];
my @preamble;
my @postamble;
my %ssl_options;
push @preamble, @{ $options{ preamble } } if $options{ preamble };
push @postamble, @{ $options{ postamble } } if $options{ postamble };
my @setup_ua = ('');
my $request_args = join ", ",
'$r',
$self->_pairlist([
maybe ':content_file', $self->output
], '')
;
my $init_cookie_jar = $self->_init_cookie_jar_mojolicious();
if( my $p = $init_cookie_jar->{preamble}) {
push @preamble, @{$p}
};
my @ssl;
if( $self->insecure ) {
push @ssl, insecure => 1,
};
if( $self->cert ) {
push @ssl, cert => $self->cert,
};
if( $self->show_error ) {
push @postamble,
' die $res->message if $res->is_error;',
} elsif( $self->fail ) {
push @postamble,
' exit 1 if !$res->is_error;',
};
my $socket_options = {};
if( my $host = $self->local_address ) {
$socket_options->{ LocalAddr } = $host;
}
my $constructor_args = join ",",
$self->_pairlist([
@ssl,
keys %$socket_options ? ( socket_options => $socket_options ) : (),
maybe request_timeout => $self->timeout,
maybe max_response_size => $self->max_filesize,
maybe cookie_jar => $init_cookie_jar->{code},
maybe SSL_options => keys %ssl_options ? \%ssl_options : undef,
], '')
;
if( defined( my $credentials = $self->credentials )) {
my( $user, $pass ) = split /:/, $credentials, 2;
my $setup_credentials = sprintf qq{\$ua->userinfo("%s","%s");},
quotemeta $user,
quotemeta $pass;
push @setup_ua, $setup_credentials;
};
@setup_ua = ()
if @setup_ua == 1;
@preamble = map { "$options{prefix} $_\n" } @preamble;
@postamble = map { "$options{prefix} $_\n" } @postamble;
@setup_ua = map { "$options{prefix} $_\n" } @setup_ua;
my $content = $self->_build_quoted_body();
if( @{ $self->form_args }) {
my $req = HTTP::Request::Common::POST(
'https://example.com',
Content_Type => 'form-data',
Content => $self->form_args,
);
$content ||= $self->_build_quoted_body( $req->content );
$self->headers->{ 'Content-Type' } = join "; ", $req->headers->content_type;
}
return <<SNIPPET;
@preamble
my \$ua = Mojo::UserAgent->new($constructor_args);@setup_ua
my \$tx = \$ua->build_tx(
'@{[$self->method]}' => '@{[$self->uri]}',
{
@{[$self->_build_mojolicious_headers(' ', %options)]}
},
$content
);
my \$res = \$ua->start(\$tx)->result;
@postamble
SNIPPET
};
sub as_rest_client_snippet( $self, %options ) {
$options{ prefix } ||= '';
$options{ implicit_headers } ||= [];
my @preamble;
my @postamble;
my %ssl_options;
push @preamble, @{ $options{ preamble } } if $options{ preamble };
push @postamble, @{ $options{ postamble } } if $options{ postamble };
my @setup_ua = ('');
my $request_args = join ", ",
'$r',
$self->_pairlist([
maybe ':content_file', $self->output
], '')
;
my $init_cookie_jar = $self->_init_cookie_jar_tiny();
if( my $p = $init_cookie_jar->{preamble}) {
push @preamble, @{$p}
};
if( $self->show_error ) {
push @postamble,
' die $res->{reason} if !$res->{success};',
} elsif( $self->fail ) {
push @postamble,
' exit 1 if !$res->{success};',
};
my $constructor_args = join ",",
$self->_pairlist([
maybe timeout => $self->timeout,
maybe host => $self->host,
maybe cert => $self->cert,
maybe timeout => $self->timeout,
], '')
;
if( defined( my $credentials = $self->credentials )) {
my( $user, $pass ) = split /:/, $credentials, 2;
my $setup_credentials = sprintf qq{\$ua->credentials("%s","%s");},
quotemeta $user,
quotemeta $pass;
push @setup_ua, $setup_credentials;
};
@setup_ua = ()
if @setup_ua == 1;
@preamble = map { "$options{prefix} $_\n" } @preamble;
@postamble = map { "$options{prefix} $_\n" } @postamble;
@setup_ua = map { "$options{prefix} $_\n" } @setup_ua;
my @content = $self->_build_quoted_body();
if( grep {/\S/} @content ) {
unshift @content, 'content => ',
} elsif( @{ $self->form_args }) {
my $req = HTTP::Request::Common::POST(
'https://example.com',
Content_Type => 'form-data',
Content => $self->form_args,
);
@content = ('content => ', $self->_build_quoted_body( $req->content ));
$self->headers->{ 'Content-Type' } = join "; ", $req->headers->content_type;
}
return <<SNIPPET;
@preamble
my \$ua = REST::Client->new($constructor_args);@setup_ua
my \$res = \$ua->request(
'@{[$self->method]}' => '@{[$self->uri]}',
{
headers => {
@{[$self->_build_rest_client_headers(' ', %options)]}
},
@content
},
);
@postamble
SNIPPET
};
=head2 C<< $r->as_curl >>
print $r->as_curl;
Returns a curl command line representing the request
This is convenient if you started out from something else or want a canonical
representation of a curl command line.
=over 4
=item B<curl>
The curl command to be used. Default is C<curl>.
=back
=cut
# These are what curl uses as defaults, not what Perl should use as default!
our %curl_header_defaults = (
'Accept' => '*/*',
( run in 0.526 second using v1.01-cache-2.11-cpan-13bb782fe5a )