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

am trying to parse blockchain wallet service api, but am getting this error

malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "Can't connect to 127...") at test.pl line 18.

Thanks Appreciate for help

Blockchain Wallet Service API Create Wallet API Create blockchain wallets programmatically The create_wallet method can be used to create a new blockchain.info b +itcoin wallet. URL: http://localhost:3000/api/v2/create Method: POST or GET $password - The password for the new wallet. Must be at least 10 chara +cters in length. $api_code - An API code with create wallets permission. $priv - A private key to add to the wallet (Wallet import format prefe +rred). (Optional) $label - A label to set for the first address in the wallet. Alphanume +ric only. (Optional) $email - An email to associate with the new wallet i.e. the email addr +ess of the user you are creating this wallet on behalf of. (Optional) Please create an API code here including permissions to "Create Wallet +s". Response: 200 OK, application/json { "guid": "4b8cd8e9-9480-44cc-b7f2-527e98ee3287", "address": "12AaMuRnzw6vW6s2KPRAGeX53meTf8JbZS", "label": "Main address" }
#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use JSON::XS; my $password = "wa111110993"; my $api_code = "asdsadas"; my $url = 'http://127.0.0.1:3000/api/v2/create'; my $ua = LWP::UserAgent->new(); my $response = $ua->post( $url, { 'password' => $password, 'api_code' + => $api_code,}); my $Rep = JSON::XS->new->decode ($response->content); my $Guid = $Rep->{guid}; my $Address = $Rep->{address}; print "Guid is: $Guid\n"; print "Address is: $Address\n";

Replies are listed 'Best First'.
Re: Blockchain error
by Anonymous Monk on Jan 29, 2022 at 11:16 UTC
    "Can't connect to 127..."
    Whatever service your code expects to be running at 127.0.0.1:3000, isn't there. You forgot to check for $response->is_success and are trying to parse the error message as JSON output from that service that isn't running. Get it running first, or provide the correct address (not localhost:3000) if it is actually running.