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

API curl -H "Content-Type: text/plain" --data '{"key":"[your-key-here]","a +ddr":"183qrMGHzMstARRh2rVoRepAd919sGgMHb","callback":"https://mystore +.com?invoice_id=123","onNotification":"KEEP", "op":"RECEIVE", "confs" +: 5}' https://api.blockchain.info/v2/receive/balance_update

I GET 400 Bad Request

#!/usr/bin/perl -w use LWP::UserAgent; use JSON; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); my $ua = LWP::UserAgent->new; my $url = "https://api.blockchain.info/v2/receive/balance_update"; my $json = encode_json { key => 'fdsrr34324234', addr => '183qrMGHzMstARRh2rVoRepAd919sGgMHb', callback => 'https://mystore.com', onNotification => 'DELETE', op => 'RECEIVE', confs => '0', }; my $req = HTTP::Request->new(POST => $url); $req->content_type('text/plain'); $req->content($json); my $response = $ua->request($req); print "Content-type: text/html\n\n"; if ($response->is_success) { print $response->content; } else { print $response->status_line; }

Replies are listed 'Best First'.
Re: 400 bad request curl post
by Corion (Patriarch) on Dec 06, 2018 at 08:08 UTC

    Automatic conversion of your curl command line to Perl using HTTP::Request::FromCurl (via curl-to-lwp gives the following:

    #!perl use strict; use warnings; use WWW::Mechanize; use HTTP::Request; my $ua = WWW::Mechanize->new(); my $r = HTTP::Request->new( 'POST' => 'https://api.blockchain.info/v2/receive/balance_update', [ 'Accept' => '*/*', 'Host' => 'api.blockchain.info:443', 'User-Agent' => 'curl/7.55.1', 'Content-Length' => '169', 'Content-Type' => 'text/plain', ], '{"key":"[your-key-here]","addr":"183qrMGHzMstARRh2rVoRepAd919sGgMHb", +"callback":"https://mystore.com?invoice_id=123","onNotification":"KEE +P", "op":"RECEIVE", "confs": 5}' ); my $res = $ua->request( $r, ); __END__ Created from curl command line curl -H "Content-Type: text/plain" --data '{"key":"[your-key-here]","a +ddr":"183qrMGHzMstARRh2rVoRepAd919sGgMHb","callback":"https://mystore +.com?invoice_id=123","onNotification":"KEEP", "op":"RECEIVE", "confs" +: 5}' https://api.blockchain.info/v2/receive/balance_update

    You'll note how the request is different from your setup of the request. Most of that is likely due to not duplicating the parameters faithfully.

      thanks Corion

        i get same error. when i run the code. Error POSTing https://api.blockchain.info/v2/receive/balance_update: Bad Request at 11.pl line 20.
Re: 400 bad request curl post
by hippo (Archbishop) on Dec 05, 2018 at 23:42 UTC

    You have not transcribed the values for callback, onNotification and confs correctly.

Re: 400 bad request curl post
by 1nickt (Canon) on Dec 06, 2018 at 00:33 UTC

    Hi,

    use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

    Why?


    The way forward always starts with a minimal test.

      i use it to output errors and warnings to a browser. when am developing web app

        I know what the module is used for, but this is your client code. What does it do here?


        The way forward always starts with a minimal test.
Re: 400 bad request curl post
by stevieb (Canon) on Dec 05, 2018 at 21:52 UTC

    What doesn't work... the Perl or the curl?

    Your subject line nor your post clarify what the problem is.

      am getting 400 Bad Request on my perl code

      am parsing this API

      API curl -H "Content-Type: text/plain" --data '{"key":"[your-key-here]","a +ddr":"183qrMGHzMstARRh2rVoRepAd919sGgMHb","callback":"https://mystore +.com?invoice_id=123","onNotification":"KEEP", "op":"RECEIVE", "confs" +: 5}' https://api.blockchain.info/v2/receive/balance_update

      and the output must be like this

      Response: 200 OK, application/json { "id" : 70, "addr" : "183qrMGHzMstARRh2rVoRepAd919sGgMHb", "op" : "RECEIVE", "confs" : 5, "callback" : "https://mystore.com?invoice_id=123", "onNotification" : "KEEP" }

        Pardon me. I've got a bit of a virus and am not feeling 100%.

        Because of that, perhaps I'm not seeing the relevance to Perl here. What am I missing?

        How is this related to Perl? It looks like a JSON response, if anything.

        Pardon me if I am truly missing the Perl link here.

Re: 400 bad request curl post
by talexb (Chancellor) on Dec 09, 2018 at 14:10 UTC

    I'm a bit late to the party, but here's my two cents: Create a useful script that lets you send stuff to the API as a testbed. Here's some code I use to talk to Freshdesk:

    #!/usr/bin/perl use strict; use warnings; # 2018-0425: Test script to access the Freshdesk API. ... use LWP::UserAgent; use JSON; use Data::Dumper; use utf8; # Path to authentication module ... { my $ua = LWP::UserAgent->new; my $headers = $ua->default_headers; $ua->default_header( 'Accept', 'application/json' ); $ua->default_header( 'Accept-Charset', 'UTF-8' ); $headers->authorization_basic( $Auth::User, $Auth::Password ); binmode STDOUT, ":utf8"; foreach my $arg (@ARGV) { # 2018-0426: If it's a query, then that stuff has to be within # double quotes. The command line processing throws them out. if ( $arg =~ /^(.+query=)(.+)/ ) { $arg = join ( '', $1, '"', $2, '"' ); } my $this_url = join ( '', $Auth::Url, $arg ); print "GET $this_url ..\n"; my $response = $ua->get($this_url); if ( $response->is_success ) { my $result = decode_json( $response->content ); print Dumper ($result); if ( exists ( $result->{'description_text'} ) ) { my $as_is = $result->{'description_text'}; print "Text as-is: $as_is.\n"; } } else { print "Ugh, not a good response.\n"; print "Response:\n" . " " . $response->status_line . "\n" . " " . $response->message . "\n" . " " . $response->content . "\n"; } } }

    I don't know if this is applicable to your situation, but it's a way of handling good and bad responses as a way of testing what works and what doesn't with an API. I've used this approach with Freshdesk, BigCommerce and Stripe. You'll probably find that some adjustments are necessary.

    Also note that I've put my authentication (URL, user and password) into a separate module .. that's a really good idea, in case you end up copying and pasting your code on-line. That module is in another directory that's not under source control -- because there's no reason to put that kind of sensitive information into version control.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.