Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

REST::Client POST help

by ogrp (Initiate)
on Nov 21, 2019 at 17:02 UTC ( [id://11109005]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

Very newbie here so any help appreciated. We have the following API and information.

https://api.sixfold.com/v1/companies/{company_id}/carrier-shipments/{shipment_id}/allocation

application/json { "type": "vehicle", "vehicle_id": "123" }

What am I doing wrong below in the POST command? It returns "Forbidden", which in this case means there is something awry with the format. The headers seem to be OK.

use REST::Client; use JSON; use Data::Dumper; my $client = REST::Client->new(); $client->POST ('https://api.sixfold.com/v1/companies/296/carrier-shipm +ents/0005360404/allocation','{"type":"vehicle", "vehicle_id":"39257" +}',{Authorization => 'Bearer 096dd6e084d5f30fazzz', "Content-type" => + 'application/json'}); print Dumper($client->responseContent());

Replies are listed 'Best First'.
Re: REST::Client POST help
by Don Coyote (Hermit) on Nov 21, 2019 at 19:12 UTC

    The POD shows the content and headers wrapped in an anonymous array. Does that work?

    $client->POST ('https://api.sixfold.com/v1/companies/296/carrier-shipm +ents/0005360404/allocation', [ '{"type":"vehicle", "vehicle_id":"39257" +}', {Authorization => 'Bearer 096dd6e084d5f30fazzz', "Content-type" => + 'application/json'} ] );

    REST::Client

    =head3 POST ( $url, [$body_content, %$headers] ) Preform an HTTP POST to the resource specified. Takes an optional body + content and hashref of custom request headers. =cut

    Nope thats just indicating optional arguments, sorry.

    There's some faffing with checking if the content is a scalar ref, maybe something to do with that?

    update The content appears to be getting set from REST::Client. It's not passed in on the new so it is not set at construction time. Although the variable $http_req{_content} is set to the empty string at this time, so it exists.

    The subroutine then queries if wantarray is defined. Although I don't think it is, if it is it will set the content and return the empty string. If not defined it will just set the content. If it is being called in void context it should Carp on global warnings being set so you'd be aware of that, also that would have been flagged before now I'm sure. Then it returns.

    The set_content routine does a utf8_downgrade, but I don't think there are any problematic characters in that string. Then sets the string as the content.

    Then it moves on to setting the headers and custom_headers, but you already said its not the headers.

    So I can't see anything from reading through that would be a problem as such.

    Aside from that, I was not sure exactly how the lc function in the auto getter/setter is working, and might ask someone to look at that for a second opinion.

    update 2:From this point I could suggest sending a POST request from HTTP::Request directly and comparing the results, working back from there. HTTP::Request provides a dump routine, but its not documented so use at own risk.

    #HTTP::Request POD =head2 Simple POST Here, we'll create a simple POST request that could be used to send JS +ON data to an endpoint. #!/usr/bin/env perl use strict; use warnings; use Encode qw(encode_utf8); use HTTP::Request (); use JSON::MaybeXS qw(encode_json); my $url = 'https://www.example.com/api/user/123'; my $header = ['Content-Type' => 'application/json; charset=UTF-8'] +; my $data = {foo => 'bar', baz => 'quux'}; my $encoded_data = encode_utf8(encode_json($data)); my $r = HTTP::Request->new('POST', $url, $header, $encoded_data); # at this point, we could send it via LWP::UserAgent # my $ua = LWP::UserAgent->new(); # my $res = $ua->request($r); =cut

    Maybe that can help you back engineer into the REST::Client framework to get the response you are after. Formatting the data using the encode routines suggested in the pod could be a good start.

Re: REST::Client POST help
by Anonymous Monk on Nov 22, 2019 at 10:38 UTC

    Hi

    For info, there is an authorisation header sent. This has been resolved. It was not a perl issue but one of the data items in the URL was incorrect. The API owners finally got back to me.

    However, really appreciate the alternative way to POST. It gives a lot more diagnostic data than the way I used which was helpful in diagnosing the issue.

Re: REST::Client POST help
by The_Dj (Beadle) on Nov 22, 2019 at 00:49 UTC
    Uhh.... Your error is 'Forbidden'
    I don't see any user credentials... Do you need to log in?

    Can you execute the action with curl or some other tool?

    If there is a SWAGGER frontend for this site, that's a god place to poke about, too.
      I don't see any user credentials... Do you need to log in?

      If you look closely at ogrp's code you will see this key-value pair:

      Authorization => 'Bearer 096dd6e084d5f30fazzz'

      which is being sent as part of the headers. While this may be wrong or right (invalid or valid, auth or mechanism), there is at least some evidence of an attempt to authenticate.

        missed that.
        soory.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-03-28 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found