Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: parsing curl api

by frank1 (Beadle)
on Mar 23, 2022 at 16:05 UTC ( [id://11142334]=note: print w/replies, xml ) Need Help??


in reply to Re: parsing curl api
in thread parsing curl api

it works, but the problem is that, what is x2520 and the rest in that format, beacuse this sends sms its sms channel

Body=This\x2520is\x2520the\x2520ship\x2520that\x2520made\x2520the\x2520Kessel\x2520Run\x2520in\x2520fourteen\x2520parsecs\x253FFrom=\x252B15017122661&To=\x252B15558675310 when i try to make like whatsapp:<sandbox phone number>. as referenced from there https://www.twilio.com/docs/api/errors/63007 its seems thats am making the request body wrong

so how can i make this body

To=whatsapp:+256775562361& From=whatsapp:+14155238886

because the code is ok and working but the problem is body formating

your body looks like this

To=+256775562361& From=+14155238886

from this

<code>Body=This\x2520is\x2520the\x2520ship\x2520that\x2520made\x2520th +e\x2520Kessel\x2520Run\x2520in\x2520fourteen\x2520parsecs\x253FFrom=\ +x252B15017122661&To=\x252B15558675310

Replies are listed 'Best First'.
Re^3: parsing curl api
by Corion (Patriarch) on Mar 23, 2022 at 16:55 UTC

    The \x2520 means that a space character (originally encoded as %20) gets written as \x2520, that is, as \x25 (the percent sign) and then the 20.

    I think you will need to properly URL-encode your payload, but I don't know what parts you actually need to change.

    I think in my example, you can simply replace all \x25 by % and it will still work. But all + will need to be encoded as %2b, because that's how URL-encoded stuff works.

    The main problem that I now see in your code is that you are constructing an URL that contains the form parameters. This is not how POST requests work. You need to put the form parameters in the request body. Most likely something like the following will work:

    my %form = ( To => "+1234", From => "+5678", Body => "Hello Friend", ); my $res = $ua->post( $url, \%form );

      i tried this but also not working

      #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); print "Content-type: text/html\n\n"; my $ua = LWP::UserAgent->new(); my $url = "https://api.twilio.com/2010-04-01/Accounts/AC9c1ff2d1880e4a +ff9d16d3a033b4631c/Messages.json"; my %form = ( To => "+156775562300", From => "+14155238886", Body => "Hello Friend", ); my $response = $ua->post( $url, 'Authorization' => 'Basic QUM5YzFmZjJkMTg4MGU0YWZmOWQxNmQzYTAzM2I0 +NjMxYzplYmFjZTRhYmQ0ZmY1NGM5NDNlZGFlZTg0YjJhMGY0MA==', 'Content-Type' => 'application/x-www-form-urlencoded', 'Content' => %form, ); if ($response->is_success) { print $response->decoded_content; } else { print $response->decoded_content; }

      it does not read to phone number.

      {"code": 21604, "message": "A 'To' phone number is required.", "more_info": "https://www.twilio.com/docs/errors/21604", "status": 400}

      but your first example code worked very well, but only was for sms not whatsapp, the only problem was constructing this body

      To=whatsapp:+2123456789&From=whatsapp:+14155238886&Body=hey test msg

      because this worked very well, but its not the request i want to send. you bypassed my question

      Body=This\x2520is\x2520the\x2520ship\x2520that\x2520made\x2520the\x2520Kessel\x2520Run\x2520in\x2520fourteen\x2520parsecs\x253F&From=\x252B15017122661&To=\x252B15558675310

        You should see some warnings. The Content key takes a hash reference, not a hash (See post of LWP::UserAgent):

        my $response = $ua->post( $url, 'Authorization' => 'Basic QUM5YzFmZjJkMTg4MGU0YWZmOWQxNmQzYTAzM2I0 +NjMxYzplYmFjZTRhYmQ0ZmY1NGM5NDNlZGFlZTg0YjJhMGY0MA==', 'Content-Type' => 'application/x-www-form-urlencoded', 'Content' => \%form, # note the backslash here! );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11142334]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found