As part of simulating what the front-end of an application will do while I work on the backend, I have been running various curl commands. Was easy to get just a file to be sent as Content-Type:application/octet-stream
or just json with Content-Type:application/json
I sent json inline with this:
curl -X POST -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://127.0.0.1:8088
Pulled the json out of a file and sent it like this:
curl -X POST -H "Content-Type: application/json" -H 'Accept: application/json' --data-binary @test.json http://127.0.0.1:8088
(any ideas what the 'Accept' does? does not work without it.., solution from here)
Sent just a file by itself like this:
curl --request POST -H "Content-Type:application/octet-stream" --data-binary "@photo.jpg" http://127.0.0.1:8088
Multipart with json inline and a picture goes very nicely like this:
curl --verbose --request POST --header "Content-Type:multipart/form-data" --form key1=value1 --form upload=@photo.jpg http://127.0.0.1:8088
(solution from here)
The trouble starts when I try to pull both the key-value pairs from a file and the photo, or to paste json into the curl command which also uploads a file. Can curl
be convinced to send "Content-Type:multipart/form-data"
with key value pairs coming from a file and a file attachment coming from a file?
john.json
{
"surname" : "Doe",
"name" : "John",
"city" : "Manchester",
"address" : "5 Main Street",
"hobbies" : ["painting","lawnbowls"]
}
and john.jpg
I have tried some things but it just gives me error messages:
Tried inline, pasting in json:
$ curl --verbose --request POST --header "Content-Type:multipart/form-data" --data '{"surname" : "Doe","name" : "John","city" : "Manchester","address" : "5 Main Street", "hobbies" : ["painting","lawnbowls"]}' --form upload=@john.jpg http://127.0.0.1:8088
Warning: You can only select one HTTP request method! You asked for both POST
Warning: (-d, --data) and multipart formpost (-F, --form).
Then I tried to get them both from a file, but it didn't like that either:
$ curl --verbose --request POST --header "Content-Type:multipart/form-data" --form upload@john.json --form upload=@john.jpg http://127.0.0.1:8088
Warning: Illegally formatted input field!
curl: option --form: is badly used here
curl: try 'curl --help' or 'curl --manual' for more information
Any ideas how to make this work?