Curl GET request with json parameter

JsonRestCurl

Json Problem Overview


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.

Json Solutions


Solution 1 - Json

This should work :

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

use option -i instead of x.

Solution 2 - Json

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.

Solution 3 - Json

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

Solution 4 - Json

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.

Solution 5 - Json

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.

Solution 6 - Json

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"}}]}'

Solution 7 - Json

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPradeep SimhaView Question on Stackoverflow
Solution 1 - JsonHarshal BulsaraView Answer on Stackoverflow
Solution 2 - JsonSteven SorokaView Answer on Stackoverflow
Solution 3 - JsonMartin SeelerView Answer on Stackoverflow
Solution 4 - Jsonuser2877889View Answer on Stackoverflow
Solution 5 - JsonsmciView Answer on Stackoverflow
Solution 6 - JsonSandeep K VView Answer on Stackoverflow
Solution 7 - Jsonim_bhatmanView Answer on Stackoverflow