http://qs321.pair.com?node_id=11118998

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

Hi,

I am trying to connect Quest Foglight using REST::Client over https. I tried in PostMan tool and it is working over there but it accept authToken in Request Body.

Appreciate if someone could help me to understand how to pass "authToken" in PERL code as Request Body. Below is my code FYR. This is not working as I am new to PERL and don't know how to pass authToken as request body.

Use REST::Client; my $client = REST::Client->new(); $req = '{"authToken":"<My Auth Token Value>"}'; # Above value should be passed as request Body. As per Postman Tool. $url = "https://<my Quest Server>/api/v1/security/login"; $client->POST($url,$req); print($client->responseCode()); exit();

Replies are listed 'Best First'.
Re: REST::Client + Request Body
by hippo (Bishop) on Jul 07, 2020 at 11:39 UTC
    This is not working

    Your code starts with Use REST::Client; when it should be use REST::Client; because Perl is case-sensitive and use needs to be all lowercase. It's no surprise it isn't working.

    You may also need to set some headers depending on what the server expects. Here is a fully working SSCCE to get you started.

    #!/usr/bin/env perl use strict; use warnings; use REST::Client; my $rest = REST::Client->new ({ host => 'http://httpbin.org' }); my $data = '{"key":"value"}'; my $headers = { Accept => 'application/json', 'Content-type' => 'application/json', }; $rest->POST ('/anything', $data, $headers); print $rest->responseContent;

      Hi, Thanks for your prompt response.

      it was a typo as i cannot copy/paste my code here, so i wrote it manually here.

      Headers i know how to set but as I am new to PERL i don't know how to pass authToken as Request Body. Appreciate if you could help me .... how to pass this value as Request Body or may be share some URL from where i could learn this.

      regards, Ravi

        In my SSCCE the request body is held in $data. It is in a format called JSON. If the server expects a JSON payload then you just need to construct one along the same lines and send it.

Re: REST::Client + Request Body
by perlfan (Vicar) on Jul 07, 2020 at 20:02 UTC
    Looks like a header value, but even if it is in the BODY, HTTP::Tiny plus a JSON module would give you a lot more control over what it is you're doing.

      Finally it is working. thanks for all help and pointers shared with me which helped me to learn a lot and make it work. For rest of community users (like me 🙂 ) below are the issues and resolutions.

      From the Quest REST API documentation,

    • I didn't find what all supported headers should be there in PERL code to support the REST Call.
    • No where was mentioned that authToken OR user/password need to be supplied as Request Body/Content.
    • May be i missed in those things in documentation or due to as i have limited knowledge with Quest and PERL 🙂
    • Anyway, I tried POSTMAN and downloaded the QUEST collection and started working on it and then came to know about above issues and corrective actions and it worked afterwards. For community users, below is the short code for authentication:

      use REST::Client; use JSON qw(decode_json); use Data::Dumper; my $client = REST::Client->new(host => 'https://<Your Host>:<APIPort>/ +api/v1'); $req = "authToken=<Authentication Token Value>"; $headers = { Accept => '*/*', 'Content-Type' => 'application/x-www-form-urlencoded', }; #Below 2 lines are to bypass the SSL verification which was also causi +ng authentication issues. $client->getUseragent()->ssl_opts(verify_hostname => 0); $client->getUseragent()->ssl_opts(SSL_verify_mode => SSL_verify_NONE); $client->POST('/security/login',$req,$headers); print($client->responseCode()); print($client->responseContent()); exit();
        #Below 2 lines are to bypass the SSL verification which was also causi +ng authentication issues. $client->getUseragent()->ssl_opts(verify_hostname => 0); $client->getUseragent()->ssl_opts(SSL_verify_mode => SSL_verify_NONE);

        It's dangerous not to verify the certificates, it makes a man-in-the-middle attack possible. If this is for production code, fix the cause instead of silencing the symptoms.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]