I have tried only GET calls using curl, and now I need to make a POST call. I need to give the parameters or input using JSON. How do I write this command?
2 Answers
Assuming you are asking how to format a curl POST request with JSON attributes.
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d "{'json':{'key':'value'}}" http://your.domain/endpoint/here
This page covers a lot of useful cases.
4 Comments
Vishnu Ranganathan
Thanks Jack and Hrishikesh your answer helped me a lot . i formed the following curl command : curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d "{'json':{"Name": "web","TaskTemplate":{"ContainerSpec":{"Image": "nginx:alpine","Mounts":[{"VolumeOptions":{"DriverConfig":{}}}],"User": "33"},"LogDriver":{"Name": "json-file","Options":{"max-file": "3","max-size": "10M"}}}}}" https://<IP-Address>:2376/services/create . but it throwed me 500 error, can you look back where i made a mistake that would help me a lot
Jack Bracken
Your API probably doesn't expect your json to start with a key named json, so remove that. { "Name": "web", "TaskTemplate": { "ContainerSpec": { "Image": "nginx:alpine", "Mounts": [{ "VolumeOptions": { "DriverConfig":{}}}], "User": "33" }, "LogDriver": { "Name": "json-file", "Options": { "max-file": "3", "max-size": "10M" }}}}
Vishnu Ranganathan
i tried that jack but it didn't solve the issue . below is the error message i am getting : HTTP/1.1 500 Internal Server Error Content-Type: application/json Server: Docker/1.12.2 (linux) Date: Fri, 21 Oct 2016 14:12:12 GMT Content-Length: 81
Jack Bracken
It's pretty much going to be impossible to diagnose that in the context of this question. I'd suggest asking a new question and including what application is running on the server, and the application logs. Probably it can't handle something in the given input
You need to user header option in your command as below.
-H/--header "Content-Type: application/json"
So the full command will be like: curl -H "Content-Type: application/json" -X POST -d '{"username":"abc","password":"xyz"}' http://url/to/some/server
2 Comments
Vishnu Ranganathan
i tried your format Hrishikesh. It works as expected for me thanks a lot .
Suresh
I tried to use the same format on Linux but I am getting "no data provided" error.