164

I am trying to send a "GET" request to a remote REST API from Command Prompt via cURL like this:

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

But it returns no output. I tried to ping the URL directly from the browser, I am able to get response successfully, I don't understand what is the wrong with the command.

Basically I want to set a "GET" request to a remote REST service which gives me json data as a response via curl. Can anyone guide me what mistake am I doing? I tried various posts, but all of them talk about POST requests not about GET.

Clemens Tolboom
  • 1,872
  • 18
  • 30
Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107

7 Answers7

163

This should work :

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

use option -i instead of x.

YSC
  • 38,212
  • 9
  • 96
  • 149
Harshal Bulsara
  • 7,898
  • 4
  • 43
  • 61
  • 4
    perhaps different quotation? "server:5050/a/c/getName{'param0':'pradeep'}" – A B Aug 27 '15 at 08:12
  • It should really be either `'server:5050/a/c/getName{"param0":"pradeep"}'` or `"server:5050/a/c/getName{\"param0\":\"pradeep\"}"`. – Benjamin W. May 23 '18 at 14:56
48

If you really want to submit the GET request with JSON in the body (say for an XHR request and you know the server supports processing the body on GET requests), you can:

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

Most modern web servers accept this type of request.

Steven Soroka
  • 19,404
  • 4
  • 52
  • 40
  • This does not work produce the result intended. Using https://httpbin.org/get for debugging, this yields: `{ "args": {}, "headers": { "Accept": "application/json", "Content-Length": "20", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:57.0) Gecko/20100101 Firefox/57.0" }, "origin": "27.94.235.50, 27.94.235.50", "url": "https://httpbin.org/get" }` Nothing is received. You need to use a query string like `curl -X GET https://httpbin.org/get?data=%7B%22param0%22%3A%22pradeep%22%7D` – Jacques Nov 17 '19 at 10:38
  • That depends on whether or not your web server is checking the body for GET requests, which I admit is not entirely standard behavior. You may be better off using the url query parameters like you say. One issue with using a body on a get request is that the browser can not replay the request by navigating with the browser history, though this is probably fine for XHR requests. – Steven Soroka Nov 18 '19 at 12:41
  • Alternatively what one can do, if one has enough control on the server side, is add a special property in the json data like "method": "get", send the payload in a post request and have the code on the server interpret this as a get request. – Jacques Nov 19 '19 at 13:09
  • @Jacques sure, but if you have control of the server you can just as easily have your server read bodies of GET requests. Getting back to the original question, I think this whole tangent is a bit off topic. Re-reading the question, I don't think OP has access to change the server. – Steven Soroka Nov 19 '19 at 16:40
  • yes that's true. If you control the server. My comment was spurred by the following statement you made which, like I said, would not produce the intended result: "Most modern web servers accept this type of request". Factually, they would accept the request you described, but the request would fail to produce the intended result. Or you may want to revise the statement like this: "Most modern web servers accept this type of request assuming you have direct control on the server side, but this is non-standard" – Jacques Nov 20 '19 at 22:14
17

If you want to send your data inside the body, then you have to make a POST or PUT instead of GET.

For me, it looks like you're trying to send the query with uri parameters, which is not related to GET, you can also put these parameters on POST, PUT and so on.

The query is an optional part, separated by a question mark ("?"), that contains additional identification information that is not hierarchical in nature. The query string syntax is not generically defined, but it is commonly organized as a sequence of = pairs, with the pairs separated by a semicolon or an ampersand.

For example:

curl http://server:5050/a/c/getName?param0=foo&param1=bar
Martin Seeler
  • 6,874
  • 3
  • 33
  • 45
  • 9
    Any HTTP request message is allowed to contain a message body. It is never useful for GET because of GET semantics - contents of request body, if any, shouldn't change response. – Jarek Przygódzki Jun 24 '16 at 15:35
9

GET takes 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 a regular REST should look something like

curl http://server:5050/a/c/getName/pradeep If it takes JSON in GET URL, it's not a standard way.

5

Try

curl -G ...

instead of

curl -X GET ...

Normally you don't need this option. All sorts of GET, HEAD, POST and PUT requests are invoked instead by using dedicated command line options.

This option only changes the actual word used in the HTTP request, it does not alter the way curl behaves. So for example if you want to make a proper HEAD request, using -X HEAD will not suffice. You need to use the -I, --head option.

smci
  • 32,567
  • 20
  • 113
  • 146
4

For username and password protected services use the following

curl -u admin:password -X GET http://172.16.2.125:9200 -d '{"sort":[{"lastUpdateTime":{"order":"desc"}}]}'
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Sandeep K V
  • 97
  • 2
  • 3
0

None of the above mentioned solution worked for me due to some reason. Here is my solution. It's pretty basic.

curl -X GET API_ENDPOINT -H 'Content-Type: application/json' -d 'JSON_DATA'

API_ENDPOINT is your api endpoint e.g: http://127.0.0.1:80/api

-H has been used to added header content.

JSON_DATA is your request body it can be something like :: {"data_key": "value"} . ' ' surrounding JSON_DATA are important.

Anything after -d is the data which you need to send in the GET request

im_bhatman
  • 896
  • 1
  • 18
  • 28