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

Hi I have a big json that i redirect to curl. I am putting some parts of it.
my $postjson = ''; ## a big input normally my $response= open(PIPIE,'|curl -d @- destination') or die "smth"; print PIPIE $postjson; close PIPIE;
curl command returns a success and when I run the script it prints to stderr and I can see it on console. How to capture response of curl and why it prints stdout of curl command to stderr of my script. What is the correct way to do this with pipes.

Replies are listed 'Best First'.
Re: capturing stdout of pipe
by hippo (Archbishop) on Nov 14, 2019 at 13:45 UTC

      Also, for converting Curl command lines to LWP (or HTTP::Tiny), see HTTP::Request::FromCurl, or its live site at https://corion.net/curl2lwp.psgi.

      The Curl command

      curl -d '{"some":"json"}' destination

      gives

      #!perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new( 'send_te' => '0' ); my $r = HTTP::Request->new( 'POST' => 'http://destination/', [ 'Accept' => '*/*', 'Host' => 'destination:80', 'User-Agent' => 'curl/7.55.1', 'Content-Length' => '15', 'Content-Type' => 'application/x-www-form-urlencoded', ], "{\x22some\x22:\x22json\x22}" ); my $res = $ua->request( $r, ); __END__ Created from curl command line curl -d '{"some":"json"}' destination

      This should give the OP a decent starting point.

      However, in the specific case of curl, my preferred answer is: don't shell out in the first place but use a module instead.

      Yes, definitely!

      It's an FAQ! How can I open a pipe both to and from a command?

      That FAQ answer says to use IPC::Open2/3, but for my personal tastes those are a little too low-level. If one really had to do this (of which this isn't a case), I would try IPC::Run first.