plx has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to translate the following curl statement into HTTP::Tiny

curl --insecure --user user:password -XPOST -H Content-type:application/json -H Accept:application/json -d '{"nam e":"test"}'

https://<address>

I'm having difficulty with how to specify the user:password in an insecure manner.

Replies are listed 'Best First'.
Re: Not sure how to use HTTP::Tiny
by rizzo (Curate) on Mar 30, 2018 at 10:26 UTC
    I'm having difficulty with how to specify the user:password in an insecure manner.

    Just in case "--insecure manner" is meant here, you don't have to.
    --insecure and --user user:password are not related at all.
    See:
    HTTP Tiny on CPAN
    and
    Curl manpage
Re: Not sure how to use HTTP::Tiny
by Corion (Patriarch) on Mar 30, 2018 at 06:40 UTC

    What is the current code you have and how does it fail to do what you want?

    Most likely it is merely a matter of putting the user and password into the URL as basic authentication elements.

      use HTTP::Tiny; my $url = 'https://<my-url>'; my $headers = { Accept => "application/json", 'Content-Type' => "application/json", Authorization => "user:password" }; my $response = HTTP::Tiny->new->get($url, { headers => $headers }); if ($response->{success}) { my $html = $response->{content}; print "$html\n"; } else { print "failure: $response->{status}\n"; } This prints 401

        once when i was forced to do authorization myself (via tkx sockets) i used

        if ($args{user} || $args{password}) { use MIME::Base64; $tkxf_auth='Authorization: Basic '.encode_base64($args{user}.':' +.$args{password},''); }
        which would make your call
        use MIME::Base64; my $headers = { Accept => "application/json", 'Content-Type' => "application/json", Authorization => 'Authorization: Basic '.encode_base +64($args{user}.':'.$args{password},'') };

        Edit: the real userid/passwords are stored in the hash %args under keys userid and password!

        Authorization => "user:password"

        Are you sure that you want to send an "Authorization" header with exactly user:password in it?

        The usual format for Authorization headers says that it has at least a type in front of it:

        Authorization: <type> <credentials>

        So maybe now is the time to look at what data Curl would try to send over the network and then try to replicate that using Perl / HTTP::Tiny.