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

thanks to poj who helped me on this 400 BAD REQUEST to structure the request. then I came up with header structure and body structure. now am trying to make the request but am getting 500 Not a SCALAR reference

#!/usr/bin/perl use strict; use warnings; use HTTP::Request::Common qw(POST); use LWP::UserAgent; use Digest::SHA qw(hmac_sha256_hex); use URI; use Encode; use HTTP::Headers; use HTTP::Message; my $nonce = 1000; my $url = "https://localbitcoins.com"; my $api_endpoint = "/api/merchant/new_invoice/"; my $hmac_key = "sdfsdfdsfdsfy"; my $hmac_secret = "sdfdsfdsf"; my %params = ( currency => 'USD', amount => '500', description => 'HEY', internal => '0', ); my $uri = URI->new(); $uri->query_form( \%params ); my $post_params_urlencoded = $uri->as_string; $post_params_urlencoded =~ s/^[?]//; printf "params = %s\n", $post_params_urlencoded; my $message = encode('utf8',$nonce.$hmac_key.$api_endpoint.$post_p +arams_urlencoded); my $signature = uc hmac_sha256_hex($message, $hmac_secret); printf "sig = %s\n",$signature; print"\n"; print "headers are:\n"; my $headers = HTTP::Headers->new( 'Apiauth-Key' => $hmac_key, 'Apiauth-Nonce' => $nonce, 'Apiauth-Signature' => $signature, ); my $body = HTTP::Message->new($headers, $post_params_urlencoded); print $body->as_string; my $ua = LWP::UserAgent->new; $ua->agent("MyApp/0.1 "); my $req = HTTP::Request->new(POST => $url); $req->content($body); my $res = $ua->request($req); print "Content-type: text/plain\n\n"; if ($res->is_success) { print $res->content; } else { print $res->status_line, "\n"; }

Output

params = currency=USD&amount=500&internal=0&description=HEY sig = FE5B4A4A66FEAB0C1216B59C81BC2FD2CA5DC4459D84CF9A7DD8EFE37242E59A headers are: Apiauth-Key: sdfsdfdsfdsfy Apiauth-Nonce: 1000 Apiauth-Signature: FE5B4A4A66FEAB0C1216B59C81BC2FD2CA5DC4459D84CF9A7DD +8EFE37242E 59A currency=USD&amount=500&internal=0&description=HEY Content-type: text/plain 500 Not a SCALAR reference Press any key to continue . . .

Replies are listed 'Best First'.
Re: 500 Not a SCALAR reference
by 1nickt (Canon) on Jan 08, 2019 at 01:44 UTC

    Hi, you should not be mucking around with HTTP::Message directly. From the doc for HTTP::Request:

    $r->content( $bytes ) This is used to get/set the content and it is inherited from the HTTP::Message base class. See HTTP::Message for details and other methods that can be used to access the content. Note that the content should be a string of bytes. Strings in perl can contain characters outside the range of a byte. The Encode module can be used to turn such strings into a string of bytes.
    (You are passing an object, not a string of bytes.)

    See also https://metacpan.org/pod/HTTP::Request#Simple-POST.

    Hope this helps!


    The way forward always starts with a minimal test.
Re: 500 Not a SCALAR reference
by stevieb (Canon) on Jan 07, 2019 at 21:57 UTC

    What isn't a scalar reference? Not all of us can run custom web API-type calls to get the error. Including error messages and indications where in the code the error is occurring is something you ought to know by now.

      i have been doing some research on error i found out that You are missing the body of your request. A POST request is supposed to have the body first, and then the optional headers

        Is that the exact error message? Please copy and paste the *exact* error message into <code></code> tags within the Original Post.

        Also, where is use strict; and use warnings;? I've come to overlook posts without those pragmas present by people who should know better.

        -stevieb: Just Another Perl Monkey