1

I am testing my RESTFul API, and i would like to know if there is a way to upload a file and json data at the same time.

When i run this command from command line:

curl -v -X POST -d '{"label":"vacations", "date":"2011-01-03"}' -F photo=@"photo.png" http://localhost/album

I get this error: Warning: You can only select one HTTP request!

Is there really a way to accomplish this?

wguerrero
  • 13
  • 1
  • 3

3 Answers3

1

It seems that you have a conflict between

-F photo=@"photo.png"

and

-d '{"label":"vacations", "date":"2011-01-03"}'

you may use only one of this 2 options.

The best approach I think is to base64 encode the picture in a string and put it in your JSON like :

-d '{"label":"vacations", "date":"2011-01-03", "photo":"AE..."}'

and base64 decode it server side.

You can add this switch too :

-H 'Content-type:text/json'
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
0

@sputnick aproach works :).

I'll use postman (a chrome extension) to test my restful API instead of curl.

wguerrero
  • 13
  • 1
  • 3
0

you can also use -F for the metadata, see Erik Allik's answer here

something like for example:

curl -F 'json={"label":"vacations", "date":"2011-01-03"}' -F 'photo=@photo.png' http://localhost/album
Community
  • 1
  • 1
Matthias
  • 3,160
  • 2
  • 24
  • 38