2

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 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
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" }}}}
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
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
1

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

i tried your format Hrishikesh. It works as expected for me thanks a lot .
I tried to use the same format on Linux but I am getting "no data provided" error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.