Curl to Advanced rest Client - json

i need to send below curl command using rest client
curl -X POST -H "Content-Type: application/json"
\ -H "Authorization: Basic MzUzYjMwMmM0NDU3NGY1NjUwNDU2ODdlNTM0ZTdkNmE6Mjg2OTI0Njk3ZTYxNWE2NzJhNjQ2YTQ5MzU0NTY0NmM="
\ -d '{"user":{"emailAddress":"test#example.com"}, "password":"password"}'
\ 'http://localhost:8080/oauth2-provider/v1.0/users'
so far i have managed to put it like above screen but im not sure how to
put
-d '{"user":{"emailAddress":"test#example.com"}, "password":"password"}'
in rest client

there is a section below for "payload"
that is where the json that you have should be placed.
the "-H" content is for the header
the "-d" is for the contents (body)
and for what its worth, the industry standard has been moving towards postman as a common chrome plugin/tool instead of advanced rest client, but to each their own.

Related

How I can list all added apps in javamelody monitoring using curl command?

curl -vkg http://collectserver/ -H 'Authorization: Basic abc123==' -d "appName=APP_NAME&appUrls=http://myhost/myapp/"
This is working curl to add apps in javamelody monitoring
And I also tested for delete application
curl -v
"http://collectserver/collectServer?action=remove_application&application=$APP_NAME" -H 'Authorization: Basic abdvcgvh'
Now I want to create curl to list all added application using curl command plz help
You can call the same URL with the parameters "?part=applications&format=json" or "?part=applications&format=xml"
You will get a map in json, which lists the applications as keys.
For example, curl -v "http://collectserver/collectServer?part=applications&format=json" -H 'Authorization: Basic abdvcgvh'

How to get cURL to output only HTTP response body (JSON) and no other headers etc

I am using curl in a bash script to fetch the response of a service as below,
response=$(curl -isb -H "Accept: application/json" "http://host:8080/some/resource")
Service response is of json type and on browser I could perfectly fine response.
However curl response has other unwanted things (such as set-cookie, content-length header in this case) and sometimes the actual response is eaten up.
Here is the output of echo $response >
Set-Cookie: rack.session=BAh7CEkiD3Nlc3Npb25faWQGOgZFVEkiRWJlY2JiOTE2M2Q1ZWI4NThjMDdi%0AYjRiOWRjMGMxMGEwYTBkMjE3NmJhZDVjYzY4YjY4ZTlmMTE2ZGVkYWE3MTMG%0AOwBGS
SIJY3NyZgY7AEZJIiVhZmQ2MmUyZGMxMzFmOGEwMjg3NDlhNWM3YmVm%0AN2FjNwY7AEZJIg10cmFja2luZwY7AEZ7B0kiFEhUVFBfVVNFUl9BR0VOVAY7%0AAFRJIi00MTc0OGM2MWNkMzljZTYxNzY3ZjU0
Y2I5OTdiYWRkN2MyNTBkYmU4%0ABjsARkkiGUhUVFBfQUNDRVBUX0xBTkdVQUdFBjsAVEkiLWRhMzlhM2VlNWU2%0AYjRiMGQzMjU1YmZlZjk1NjAxODkwYWZkODA3MDkGOwBG%0A--ee97a62095e7d42129
tontent-Length: 354c8; path=/; HttpOnly
This is breaking my response parsing logic.
I have seen this happening intermittently which is weird.
Is there a way to get "only" json response from cURL output?
I went through the curl documentation but could not see any thing/ or I could have missed it.
You are specifying the -i option:
-i, --include
(HTTP) Include the HTTP-header in the output. The HTTP-header includes things like server-name, date of the document, HTTP-version and more...
Simply remove that option from your command line:
response=$(curl -sb -H "Accept: application/json" "http://host:8080/some/resource")
I was executing a get request an also want to see just the response and nothing else, seems like magic is done with -silent,-s option.
From the curl man page:
-s, --silent
Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute. It will still output the data you ask for, potentially even to the terminal/stdout unless you redirect it.
Below the examples:
curl -s "http://host:8080/some/resource"
curl --silent "http://host:8080/some/resource"
Using custom headers
curl -s -H "Accept: application/json" "http://host:8080/some/resource"
Using POST method with a header
curl -s -X POST -H "Content-Type: application/json" "http://host:8080/some/resource" -d '{ "myBean": {"property": "value"}}'
You can also customize the output for specific values with -w, below the options I use to get just response codes of the curl:
curl -s -o /dev/null -w "%{http_code}" "http://host:8080/some/resource"

How to call REST API using CURL & SAML TOKEN Auth

My Url is Like:
https://<ip:port>/TestRESTServices/objects/test-folder
JSON data that I want to pass is:
{
"name":"test-1",
"parent-uuid":"126"
}
test-1 is the folder name which i want to create.
When i invoke this url with the data in Poster plugin in firefox via POST it works fine and folder test-1 is created.
//using Content Type : "application/json"
How can I invoke/call this REST API using cURL ?
Need Help.
This is what i tried:
curl -i -H "Accept: application/json" -X POST -d '{"name":"test-1","parent-uuid":"126"}' https://<ip:port>/TestRESTServices/objects/test-folder
It throws an error that curl: (52) Empty reply from server
Unfortunately I don't have a REST API online to try it, but resources that I found suggest the following approaches:
curl -v -H "Content-Type: application/json" -X POST --data "#issue.json" -u login:password http://redmine/issues.json
where the issues.json is a file containing the JSON request.
Resources I found useful:
1, 2
Hope it helps!
For Authentication : Give the userid/password as admin:password
TOKEN=$(curl -s -k -X POST --basic -u "admin:password" "{host}/TestAuthServices/auth/tokens" | sed -rn 's/\{"Token":"([^"]+)".+/\1/p')
After getting this token call curl as:
curl -s -k -X POST -H "Content-Type: application/json" -H "Authorization: X-SAML ${TOKEN}" -d '{"name":"test","parent-uuid":"126"}' "{host}/TestRESTServices/objects/test-folder"

How to PUT a json object with an array using curl

I have a series of data to enter into database. The user interface to enter the data isn't good for bulk entry, so I'm trying to formulate a command line equivalent. When I examine the network request of the UI in chrome, I see a PUT request of a json object. When I try to replicate the request
curl -H 'Accept: application/json' -X PUT '{"tags":["tag1","tag2"],"question":"Which band?","answers":[{"id":"a0","answer":"Answer1"},{"id":"a1","answer":"answer2"}]}' http://example.com/service`
I get a error
curl: (3) [globbing] nested braces not supported at pos X
Where X is the character position of first "[".
How can I PUT a json object that includes an array?
Your command line should have a -d/--data inserted before the string you want to send in the PUT, and you want to set the Content-Type and not Accept.
curl -H 'Content-Type: application/json' -X PUT -d '[JSON]' \
http://example.com/service
Using the exact JSON data from the question, the full command line would become:
curl -H 'Content-Type: application/json' -X PUT \
-d '{"tags":["tag1","tag2"],
"question":"Which band?",
"answers":[{"id":"a0","answer":"Answer1"},
{"id":"a1","answer":"answer2"}]}' \
http://example.com/service
Note: JSON data wrapped only for readability, not valid for curl request.
Although the original post had other issues (i.e. the missing "-d"), the error message is more generic.
curl: (3) [globbing] nested braces not supported at pos X
This is because curly braces {} and square brackets [] are special globbing characters in curl.
To turn this globbing off, use the "-g" option.
As an example, the following Solr facet query will fail without the "-g" to turn off curl globbing:
curl -g 'http://localhost:8983/solr/query?json.facet={x:{terms:"myfield"}}'
It should be mentioned that the Accept header tells the server something about what we are accepting back, whereas the relevant header in this context is Content-Type
It's often advisable to specify Content-Type as application/json when sending JSON. For curl the syntax is:
-H 'Content-Type: application/json'
So the complete curl command will be:
curl -H 'Content-Type: application/json' -H 'Accept: application/json' -X PUT -d '{"tags":["tag1","tag2"],"question":"Which band?","answers":[{"id":"a0","answer":"Answer1"},{"id":"a1","answer":"answer2"}]}' http://example.com/service`
The only thing that helped is to use a file of JSON instead of json body text. Based on How to send file contents as body entity using cURL
Try using a single quote instead of double quotes along with -g
Following scenario worked for me
curl -g -d '{"collection":[{"NumberOfParcels":1,"Weight":1,"Length":1,"Width":1,"Height":1}]}" -H "Accept: application/json" -H "Content-Type: application/json" --user test#testmail.com:123456 -X POST https://yoururl.com
WITH
curl -g -d "{'collection':[{'NumberOfParcels':1,'Weight':1,'Length':1,'Width':1,'Height':1}]}" -H "Accept: application/json" -H "Content-Type: application/json" --user test#testmail.com:123456 -X POST https://yoururl.com
This especially resolved my error curl command error : bad url colon is first character

POST JSON over CURL with basic authentication

I am using Curl from the command line to debug a small web api I am working on. The web api expects basic authentication and a JSON object as input (POST). Currently this basic authentications works fine:
curl -i --user validuser:70e12a10-83c7-11e0-9d78-0800200c9a65 http://example.com/api.php
but I also want to send a JSON object as a POST request:
curl -i --user validuser:70e12a10-83c7-11e0-9d78-0800200c9a65 -X POST -d '{"person":{"name":"bob"}}' http://example.com/api.php
I'm getting a 400 Bad Request response with the above command, any ideas on how I bundle a json object in this POST request?
Try it with:
curl -i --user validuser:70e12a10-83c7-11e0-9d78-0800200c9a65 -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"person":{"name":"bob"}}' http://mysite.com/api.php
I've removed the json= bit in the body content.
Alternatively, this post might be helpful: How to post JSON to PHP with curl
curl --request POST \
--url http://host/api/content/ \
--header 'authorization: Basic Esdfkjhsdft4934hdfksjdf'
Don't use
$person = file_get_contents("php://input");
instead use
$person = $_POST['person'];
And if you're using curl from the command-line this is the syntax for wanting to POST json data:
curl -d 'person={"name":"bob"}'