Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

cURL and HTTP::Request

by jeanbaptiste93 (Novice)
on Aug 08, 2022 at 22:10 UTC ( #11146046=perlquestion: print w/replies, xml ) Need Help??

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

Hi, all!

I am trying to call a particular API endpoint and am being unsuccessful. I have been able to make calls to dozens of endpoints to the same API, so I was unsure as to what the issue was. I tried a cURL with the relevant params and it worked successfully. But I am being unable to replicate it to HTTP::Request.

Here's the cURL:

curl --request PUT --url 'https://[URL]/[ENDPOINT]/item?key=[KEY]&token=[TOKEN]' --header 'Content-Type: application/json' --data '{"value": {"text": "38"}}'

I have tried:

my $browser = LWP::UserAgent::JSON->new(); my $response = $browser->put( $url, Content => to_json($data) );

and:

my $request = HTTP::Request::JSON->new( 'PUT', $url, [ Accept => 'application/json', 'Content-Type' => 'application/json', ], to_json($data) ); my $browser = LWP::UserAgent::JSON->new(); my $response = $browser->request($request);

and similar solutions to no avail. I appreciate any help!

Replies are listed 'Best First'.
Re: cURL and HTTP::Request
by Discipulus (Abbot) on Aug 09, 2022 at 06:48 UTC
    Hello jeanbaptiste93 and welcome to the monastery and to the wonderful world of Perl!

    I dont know CURL but our fellow monk Corion (well really a Pope ..mhh Patriarch) wrote a fantastic web service based on his HTTP::Request::FromCurl

    So, removing the unkonwn option --url I get, using LWP::UserAgent from the dropdown menu, the following code which might be a nice starting point.

    Note that I also removed your PUT

    #!perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new( 'send_te' => '0' ); my $r = HTTP::Request->new( 'POST' => 'https://[URL]/[ENDPOINT]/item?key=%5BKEY%5D&token=%5BTO +KEN%5D', [ 'Accept' => '*/*', 'User-Agent' => 'curl/7.55.1', 'Content-Length' => '25', 'Content-Type' => 'application/json' ], "{\x22value\x22: {\x22text\x22: \x2238\x22}}" ); my $res = $ua->request( $r, ); __END__ Created from curl command line --request 'https://[URL]/[ENDPOINT]/item?key=[KEY]&token=[TOKEN]' --h +eader 'Content-Type: application/json' --data '{"value": {"text": "38 +"}}'

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Whoops - these two things are bugs/missing implementations. --url should work (but is easily fixed), and --request PUT should properly set the method on the HTTP request (which it currently doesn't). I'll add tests for that and likely release a new version soonish :)

      Update: V 0.41 has just been released and fixes the two issues

      Oh, yes. I also tried that module and had the same --url issue. I tried it with your solution, and it is working for me. You are gold! I have been stuck for this for 2 days, and now I'm getting it to work. Appreciate it a lot!!
Re: cURL and HTTP::Request
by 1nickt (Canon) on Aug 08, 2022 at 23:38 UTC

    ... so then what happened? How did it fail? What did it say?


    The way forward always starts with a minimal test.
      Oh, yes, sorry. It returns "Invalid value for custom field type", which seems to mean it is not getting the value as it expects.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2022-10-07 09:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (29 votes). Check out past polls.

    Notices?