Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

http post

by bigup401 (Pilgrim)
on Feb 19, 2017 at 14:18 UTC ( [id://1182297]=perlquestion: print w/replies, xml ) Need Help??

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

i got some issue when i execute my code i get this Response Code: 411

#!/usr/bin/perl use strict; use HTTP::Request::Common qw(POST); use LWP::UserAgent; my $ua = new LWP::UserAgent; my $senderid = "demo"; my $num = "447777777777"; my $sms = "demo"; my $rep = HTTP::Request->new(POST => "http://api.website.com/api/v1/sm +s/send/?apiKey=edftr44456&message=$sms%20HTTP%20API&from=$senderid&to +=$num"); $rep->content_type("application/x-www-form-urlencoded"); $rep->content_type("Content-Type' => 'application/json"); my $repobj = $ua->request($rep); my $repcode = $repobj->code; print 'Response code: ' . $repcode . "\n";

Replies are listed 'Best First'.
Re: http post
by 1nickt (Canon) on Feb 19, 2017 at 14:55 UTC

    Hi bigup401,

    HTTP code 411 means The server refuses to accept the request without a defined Content-Length. You're probably getting this because you are attempting to make a POST request but not sending any body, while the API you are querying expects a GET request with params in the URL query string (which you've sorta done).

    Update: It's unclear whether you are supposed to be making a POST or a GET request. ( Note that the URL you have has been replaced, according to the 301 response I got when testing further. The updated URL for the API endpoint is https://www.website.com/api/v1/sms/send. Most user agents will follow redirects but you might as well update the URL in your source code. This does suggest that you are not working from the latest documentation for the API you are attempting to use, however.)

    Here are two examples that are more like what you need (using HTTP::Tiny, which can simplify your life considerably for simple tasks). Showing with GET and with POST:

    use strict; use warnings; use HTTP::Tiny; my $ua = HTTP::Tiny->new; my $url = "https://www.website.com/api/v1/sms/send"; my $data = { apiKey => "edftr44456", from => "demo", to => "447777777777", message => "demo HTTP API", }; my $params = $ua->www_form_urlencode( $data ); my $response = $ua->get( $url . '/?' . $params ); print $response->{'status'} . "\n";
    use strict; use warnings; use HTTP::Tiny; my $ua = HTTP::Tiny->new; my $url = "https://www.website.com/api/v1/sms/send"; my $data = { apiKey => "edftr44456", from => "demo", to => "447777777777", message => "demo HTTP API", }; my $response = $ua->post_form( $url, $data ); print $response->{'status'} . "\n";
    Unfortunately both these code examples produce an HTTP status code of 404, which suggests that you aren't using the correct URL/params for the API.

    Hope this helps!


    The way forward always starts with a minimal test.

      thanks hippo thanks 1nickt. 1nickt method has worked for me

      use strict; use warnings; use HTTP::Tiny; my $ua = HTTP::Tiny->new; my $url = "https://www.website.com/api/v1/sms/send"; my $data = { apiKey => "edftr44456", from => "demo", to => "447777777777", message => "demo HTTP API", }; my $params = $ua->www_form_urlencode( $data ); my $response = $ua->get( $url . '/?' . $params ); print $response->{'status'} . "\n";

      i will use HTTP::Tiny

      https://api.kayesms.com/api/v1/sms/send/

      thats the valid link

      i dont know why. all method should work but i think its the api they use HTTP 1/1

      because direct input works

      https://api.kayesms.com/api/v1/sms/send/?apiKey=9F607F4A-00F2-4B06-A40A-F4B6A574915C&message=Testing%20HTTP%20API&from=test&to=447868756765,44786875676

      all methods of http post ther should work, bt i think the problem with api

Re: http post
by hippo (Bishop) on Feb 19, 2017 at 14:31 UTC

      its the same when i try the same example i get the same error 411 Length Required

        Forgive my scepticism but I rather suppose that isn't the case. Here's the SSCCE you should have supplied. It gives a 301.

        #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $rep = HTTP::Request->new (POST => "http://api.website.com/api/v1/s +ms/send/"); $rep->content_type ("application/json"); $rep->content ('{ "foo": "bar" }'); my $res = $ua->request($rep); print 'Response code: ' . $res->code . "\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-26 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found