Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

parsing curl api

by frank1 (Beadle)
on Mar 23, 2022 at 14:51 UTC ( [id://11142325]=perlquestion: print w/replies, xml ) Need Help??

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

Am trying to use twilio curl api

but am getting this error:

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

here is the code

#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; print "Content-type: text/html\n\n"; my $ua = LWP::UserAgent->new; my $sid = "sid"; my $auth = "auth_token"; my $url = URI->new("https://api.twilio.com/2010-04-01/Accounts/$sid/Me +ssages.json"); $url->userinfo("$sid:$auth"); $url->query_form( 'To' => 'whatsapp:+2123456789', 'From' => 'whatsapp:+14155238886', 'Body' => 'hey test msg', ); print $url, "\n"; my $req = HTTP::Request->new(POST => $url); $req->header('Content-Type' => 'application/x-www-form-urlencoded'); my $resp = $ua->request($req); if ($resp->is_success) { print $resp->decoded_content; } else { print $resp->decoded_content; }

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

    Converting the sample curl command to LWP::UserAgent using curl-to-lwp, I get the following:

    #!perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new( 'send_te' => '0' ); my $r = HTTP::Request->new( 'POST' => 'https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messag +es.json', [ 'Accept' => '*/*', 'Authorization' => 'Basic JFRXSUxJT19BQ0NPVU5UX1NJRDokVFdJTElPX0FVVEhfVE9LRU4=' +, 'User-Agent' => 'curl/7.55.1', 'Content-Length' => '129', 'Content-Type' => 'application/x-www-form-urlencoded' ], "Body=This\x2520is\x2520the\x2520ship\x2520that\x2520made\x2520the\x25 +20Kessel\x2520Run\x2520in\x2520fourteen\x2520parsecs\x253F&From=\x252 +B15017122661&To=\x252B15558675310" ); my $res = $ua->request( $r, ); __END__ Created from curl command line curl -X POST https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUN +T_SID/Messages.json --data-urlencode "Body=This is the ship that mad +e the Kessel Run in fourteen parsecs?" --data-urlencode "From=+15017 +122661" --data-urlencode "To=+15558675310" -u $TWILIO_ACCOUNT_SID:$ +TWILIO_AUTH_TOKEN

    If the above works, but your code does not, I'm not really sure where/how To goes missing...

      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

        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 );

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-28 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found