palaniraja
palaniraja

Reputation: 10492

Curl command to test the API

I need to test an API of following spec

URL: http://myapp.com/api/upload/
Method: POST
Parameters: 
   image: (binary data of image)

returns
   a JSON Object

sample php implementation to test the api

<?php
$ch = curl_init();
$url = 'http://myapp.com/api/upload/';

$fields = array('image'=>file_get_contents('profile-pic.jpg'));

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_URL, $url);

$json = curl_exec($ch);
echo "\n".$json."\n";

?>

I use RESTClient and Poster extensions to test other api, I'm not sure how to send binary data for a parameter named image with those tools.

How do I test the above api using Curl command?

No luck with this one

curl -X POST --data-binary "[email protected]" "http://myapp.com/api/upload/"

test.jpg is in current directory. I can't change the 3rd party API, to be able to test with other encoding formats.

Upvotes: 1

Views: 2879

Answers (2)

Daniel Stenberg
Daniel Stenberg

Reputation: 58004

CURLOPT_POSTFIELDS with a hash array in PHP corresponds to the command line -F option. Thus, it should be something similar to:

curl -F [email protected] http://myapp.com/api/upload/

If that isn't the exact thing, add '--trace-ascii dump.txt' to get a full trace logged and check that out closely to see what's wrong.

Upvotes: 1

Vasiliy Faronov
Vasiliy Faronov

Reputation: 12310

curl -X POST --data-urlencode '[email protected]' 'http://myapp.com/api/upload'

It’s image@, not image=@.

This is assuming that the image data is urlencoded before sending, which is what I figure the PHP code does.

Upvotes: 1

Related Questions