Curl GET request with json parameter

I am trying to send a β€œGET” request to a remote REST API from the command line via cURL as follows:

curl -X GET -H "Content-type: application/json" -H "Accept: application/json" "http://server:5050/a/c/getName/{"param0":"pradeep"}" 

But it does not return the output. I tried to check the URL directly from the browser, I can get the answer successfully, I don’t understand what is wrong with the command.

Basically, I want to set the "GET" request to a remote REST service, which gives me json data as a response via curl. Can someone guide me, what mistake am I making? I tried different posts, but they all talk about POST requests not GET.

+108
json rest curl
Jan 24 '14 at 6:59
source share
5 answers

This should work:

  curl -i -H "Accept: application/json" "server:5050/a/c/getName{"param0":"pradeep"}" 

custom parameter -i instead of x.

+127
Jan 24 '14 at 7:15
source share

If you want to send your data inside the body, you need to do POST or PUT instead of GET .

It looks like you are trying to send a request with uri parameters that is not GET related, you can also put these parameters on POST , PUT , etc.

The request is an optional part, separated by a question mark ("?"), Which contains additional identification information that is not hierarchical in nature. The query string syntax is not defined in general terms, but it is usually organized as a sequence = pairs, with pairs separated by semicolons or ampersands.

For example:

 curl http://server:5050/a/c/getName?param0=foo&param1=bar 
+15
Jan 24 '14 at 7:34
source share

GET accepts name value pairs.

Try something like:

curl http://server:5050/a/c/getName/?param1=pradeep

or

curl http://server:5050/a/c/getName?param1=pradeep

btw regular REST should look something like this:

curl http://server:5050/a/c/getName/pradeep If GET URL requires JSON, this is not a standard way.

+8
Jan 24 '14 at 7:11
source share

For services protected by username and password, use the following

 curl -u admin:password -X GET http://172.16.2.125:9200 -d '{"sort":[{"lastUpdateTime":{"order":"desc"}}]}' 
+4
Mar 21 '17 at 0:11
source share

Try

 curl -G ... 

instead

 curl -X GET ... 

Usually you do not need this option. All kinds of GET, HEAD, POST, and PUT requests are most likely called using dedicated command line options.

This option only changes the actual word used in the HTTP request; it does not change the curl behavior. For example, if you want to make the correct HEAD request, using -X HEAD will be insufficient. You need to use the -I, -head option.

+3
Aug 14
source share



All Articles