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

I am trying do a post request using REST::Client and keep getting 400 error. Here is what the client is expecting

Post: /api/m1/token

Hots: https://api.xxx.com

Content-Type: application/x-www-form-urlencoded

grant=client_credentials&partner={partner}&client={client}&secret={secret}

based on the above information what am I doing wrong. Here is the code

use Data::Dumper; use REST::Client; sub generate { my $tokenURI = 'https://api.xxx.com/api/m1/token'; displayMsg( "Establishing connection................"); my %json_body = (grant => 'client_credentials', partner => ‘dfjkdjfdkdjkdkdjfdk’, client => 'dkfjdlfjdlfkjlj', client => 'dfkljflsfjlajflafjlsdkfjdskl=', # ne +ed encoding that is = will be %3D ); $restClient->addHeader("Content-Type", "application/x-www-form-urlen +coded"); my $bodyData = encode_json(\%json_body); $restClient->POST($tokenURI, $bodyData); print Dumper($restClient); if ($restClient->responseCode() eq '200') { displayMsg ("Sucessfull "); my $token = decode_json($restClient->responseContent()); my $tokenAccess = $token->{'access_token'}; my $tokenType = $token->{'token_type'}; return $tokenType.' '.$tokenAccess; } else { displayMsg ( "Failed ."); displayMsg( 'Response status: ' . $restClient->responseCode()); foreach ( $restClient->responseHeaders() ) { print 'Header: ' . $_ . '=' . $restClient->responseHeader($_) . "\n +"; } exit 1; } }

Replies are listed 'Best First'.
Re: REST::Client POST error
by roboticus (Chancellor) on Aug 30, 2017 at 22:07 UTC

    sannag:

    Since 400 is a "Bad Request" according to Wikipedia, I'd expect that your URL isn't correct or a similar problem.

    You might try visiting the site with Chrome and using the Developer tools panel to capture an actual interaction with the site and compare it with the values you're trying to use in your code.

    Update: I just notice that your code is saying that you'll be posting form-encoded data, then you encode a JSON document, and don't include it in the POST. I think you're skipping a step or two...

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I used Chrom ''Postman' rest client and was able to post and get a reply back. Sorry about the json encoding part, corrected the code the include the $bodyData in the post url

      You are correct! I was just not smart to recognize it early on.....I was putting the content in json it should be in htttp format. I have a fix on it now and it is working :)
Re: REST::Client POST error
by huck (Prior) on Aug 30, 2017 at 23:58 UTC

    This

    Content-Type: application/x-www-form-urlencoded grant=client_credentials&partner={partner}&client={client}&secret={sec +ret}
    Makes me think it is not expecting json encoded body but a normal http format.

    via https://stackoverflow.com/questions/3635288/perl-programatically-set-post-param-using-restclient-module try

    my @body_list = (grant => 'client_credentials', partner => ‘dfjkdjfdkdjkdkdjfdk’, client => 'dkfjdlfjdlfkjlj', client => 'dfkljflsfjlajflafjlsdkfjdskl=', # ne +ed encoding that is = will be %3D ); my $bodyData=substr($restClient->buildQuery(\@body_list),1);
    im willing to bet the substr from 1 is to get rid of a leading ?

      Dude, Thank you are absolutely correct....above correction fixed it. :). I was not paying attention to Json vs http format for content
Re: REST::Client POST error
by AnomalousMonk (Archbishop) on Aug 31, 2017 at 00:19 UTC
    partner => ‘dfjkdjfdkdjkdkdjfdk’,

    Note that this statement uses  ‘ ’ "stupid" quotes that are not recognized by the Perl intepreter (at least not by any one I'm familiar with). Please use  ' single- and  " double-quotes only.


    Give a man a fish:  <%-{-{-{-<

      I converted the hash to array and used substr as recommend by huck....that fixed it.

        Usually, when "smart" | "stupid" quotes appear in posted code, it's a sign that the poster is using some kind of word-processing editor. These editors try to "help" the user by quietly pairing up all their quotes into balanced pairs of "smart" quotes. If you're programming, do yourself a favor and use a programmer's editor. (Such editors use the Humpty-Dumpty theory of language: "When I use a word," Humpty Dumpty said, in rather a scornful tone, "it means just what I choose it to mean — neither more nor less." :)

        Note: A good editor is often considered to be a part of a good IDE (Integrated Development Environment, or something like that). Maybe see this for some leads here.


        Give a man a fish:  <%-{-{-{-<

Re: REST::Client POST error
by beech (Parson) on Aug 31, 2017 at 00:26 UTC
    Hi ,

    What server are you trying to talk to (where are its docs)?

      It is a private server (unfortunately not a public domain) else would be glad to share. As Huck recommended the fix was put the content in http format (vs Json which I was doing)