Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Can't get a server response from LWP Request

by ddominnik (Novice)
on Sep 23, 2016 at 07:45 UTC ( [id://1172425]=perlquestion: print w/replies, xml ) Need Help??

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

First off, regarding Perl and coding in general I have just started to learn so it would be great if you could give me easy to understand/execute answers. Now onto the real problem:

I'm trying to create a tool for a JIRA server at my workplace. But I can't seem to get a response from the server. I already struggle with the login, which takes a JSON string as a POST request and sends back a JSON string with cookie information. I've tried through the browser and with JavaScript, that somehow works, but Perl gives me a 500 status code response. I've already tried JIRA::REST


use JIRA::REST; use JSON; my $jira = JIRA::REST->new('https://jira.hostname:PORT/jira', 'myuser' +, 'mypass'); my $request = $jira->POST("/issue/search", undef, { jql=> 'project ~ MYPROJECT and status = closed', starAt=> 0, maxResults=>1, fields=>[asignee], }); print(parse_json ($request));

Then I tried REST::Client

use REST::Client; my $client = REST::Client->new(); $client->setHost('https://jira.hostname:PORT/jira/rest'); $client->request('post', '/auth/1/session', ['{ "username" : "myus +er", "password" : "mypass" }', undef]); print ($client->responseContent());

And now I'm trying a custom request with LWP

use LWP; use LWP::UserAgent; use HTTP::Request; use JSON; my $host = 'https://jira.hostname:PORT/jira/rest'; my $loginurl = '/auth/1/session'; my %credentials = ("username"=>"myuser", "password"=>"mypa +ss"); my $json = encode_json \%credentials; my $request = HTTP::Request->new('POST', $host.=$loginurl) +; $request -> header('Content-Type'=>'application/json'); $request -> content($json); my $browser = LWP::UserAgent->new; $browser -> agent('Mozilla/5.0'); $browser -> protocols_allowed(['https']); my $response = $browser->request($request); if($response->is_success){ print $response->decoded_content; } else { die $response->status_line; };

All of them are giving me the same or a similar response:

500 Can't connect to jira.hostname:PORT Bad file descriptor at C:/myperl/perl/vendor/lib/LWP/Protocol/http.pm +line 47. at jiratest.pl line 28.

I think that the LWP lib might be broken somehow, but I already tried upgrading and reinstalling via CPAN and still got no response. The code from the LWP/Protocol/http.pm file is:

sub _new_socket { my($self, $host, $port, $timeout) = @_; # IPv6 literal IP address should be [bracketed] to remove # ambiguity between ip address and port number. if ( ($host =~ /:/) && ($host !~ /^\[/) ) { $host = "[$host]"; } local($^W) = 0; # IO::Socket::INET can be noisy my $sock = $self->socket_class->new(PeerAddr => $host, PeerPort => $port, LocalAddr => $self->{ua}{local_address}, Proto => 'tcp', Timeout => $timeout, KeepAlive => !!$self->{ua}{conn_cache}, SendTE => 1, $self->_extra_sock_opts($host, $port), ); unless ($sock) { # IO::Socket::INET leaves additional error messages in $@ my $status = "Can't connect to $host:$port"; if ($@ =~ /\bconnect: (.*)/ || $@ =~ /\b(Bad hostname)\b/ || $@ =~ /\b(certificate verify failed)\b/ || $@ =~ /\b(Crypt-SSLeay can't verify hostnames)\b/ ) { $status .= " ($1)"; } die "$status\n\n$@"; # this is the mentioned "line 47" } # perl 5.005's IO::Socket does not have the blocking method. eval { $sock->blocking(0); }; $sock; }

I really hoped I could fix this myself as generally solving issues yourself is a great way to learn about the language, but I really don't know what to do anymore. It has to be a problem on the client side, because the server is responding as regular when I'm using JavaScript. Any help would be greatly appreciated!
P.S: I've always used  use strict; use warnings; as well, so I don't think it has something to do with that


EDIT: Corion's comment was right, it was a proxy issue as well as a SSL-Certificate issue.

Thank you for your help Perl Monks!

Replies are listed 'Best First'.
Re: Can't get a server response from LWP Request
by Corion (Patriarch) on Sep 23, 2016 at 07:51 UTC

    Have you investigated whether you need to use a proxy to connect to your JIRA server? If the browser can connect but Perl cannot, maybe the browser uses a proxy.

    The second thing is to check whether the JIRA server is set up with a custom SSL certificate which your Perl code does not know about. See analyze-ssl.pl for something that can help you debug this connectivity problem.

      The proxy could be the case. Will try and report back. How do I set a proxy in Perl?
Re: Can't get a server response from LWP Request
by haukex (Archbishop) on Sep 23, 2016 at 07:56 UTC

    Hi ddominnik,

    Just a small note: in your LWP example, you need to insert the actual port number in the string my $host = 'https://jira.hostname:PORT/jira/rest';. (I haven't used JIRA::REST or REST::Client so I can't comment on those.)

    Hope this helps,
    -- Hauke D

      I did that of course, this was just a way of demonstrating my problem

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found