zoul
zoul

Reputation: 104065

How do I make curl to POST a parameter, reading its value from a file?

I’m playing with GitHub web hooks and I want to use curl to post some sample JSON. The request is supposed to contain a payload POST parameter with the JSON string as a value. As an example, this works:

$ curl --form payload='{"foo":1}' http://somewhere

But I need to read the JSON from a file (say foo.json). Replacing the JSON string by @foo.json posts the payload in the request body:

--------------------------8d0c6d3f9cc7da97
Content-Disposition: form-data; name="payload"; filename="foo.json"
Content-Type: application/octet-stream

{'foo':1}

--------------------------8d0c6d3f9cc7da97--

That’s not really what I want, I need the payload to be passed as a parameter. How do I do that?

Upvotes: 1

Views: 1318

Answers (2)

zoul
zoul

Reputation: 104065

Here’s an alternative Perl testing script. Pass the target URL as the first argument, feed the JSON to STDIN:

#!/usr/bin/env perl

use strict;
use warnings;
use LWP;

my $target = shift @ARGV or die "Need a target URL";
my $ua = LWP::UserAgent->new;
my $payload = do { local $/; <STDIN> };
my $response = $ua->post($target, { payload => $payload });
print $response->as_string;

Upvotes: 0

Piotr Kaluza
Piotr Kaluza

Reputation: 413

Maybe silly but try this:

cat foo.json | xargs -I % curl --form payload='%' http://example.com

Just wrote this little thing and it worked:

var=`cat some.json` && curl --form payload="$var" http://lvh.me/test/index.php

Upvotes: 1

Related Questions