Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

REST API + HOW TO CAPTURE THE ID NUMBER FROM API

avatar

how to capture the id request number from API ?

for example we stop the service - AMBARI_METRICS

and we get the id - 128 from output , so we can use grep to capture the id , but we prefer other elegant way ,

curl -u admin:admin -i -H 'X-Requested-By: ambari' -X PUT -d '{"RequestInfo": {"context" :"Stop AMBARI_METRICS via REST"}, "Body": {"ServiceInfo": {"state": "INSTALLED"}}}' http://localhost:8080/api/v1/clusters/sys15626/services/AMBARI_METRICS


HTTP/1.1 202 Accepted
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Cache-Control: no-store
Pragma: no-cache
Set-Cookie: AMBARISESSIONID=19rvqn2cgzwb51sfevywdfvwm7;Path=/;HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT
User: admin
Content-Type: text/plain
Vary: Accept-Encoding, User-Agent
Content-Length: 137
Server: Jetty(8.1.19.v20160209)
{
  "href" : "http://localhost:8080/api/v1/clusters/sys15626/requests/128",
  "Requests" : {
    "id" : 128,
    "status" : "Accepted"
  }
Michael-Bronson
1 ACCEPTED SOLUTION

avatar
Master Mentor

@Michael Bronson

What you are seeing in the output of the API call is actually the HTTP Response which ambari has sent to the client.

{
  "href": "http://localhost:8080/api/v1/clusters/sys15626/requests/128",
  "Requests": {
    "id": 128,
    "status": "Accepted"
   }
}

Above is a standard API response from ambari when a request is accepted. The only option to get only the ID part is to use the response parsing mechanism provided by OS like grep. (Or using Custom parsing utility / third party APIs)

.

Example:

 # cat /tmp/response.txt |  grep id | awk '{print $NF}' | cut -d , -f1

View solution in original post

1 REPLY 1

avatar
Master Mentor

@Michael Bronson

What you are seeing in the output of the API call is actually the HTTP Response which ambari has sent to the client.

{
  "href": "http://localhost:8080/api/v1/clusters/sys15626/requests/128",
  "Requests": {
    "id": 128,
    "status": "Accepted"
   }
}

Above is a standard API response from ambari when a request is accepted. The only option to get only the ID part is to use the response parsing mechanism provided by OS like grep. (Or using Custom parsing utility / third party APIs)

.

Example:

 # cat /tmp/response.txt |  grep id | awk '{print $NF}' | cut -d , -f1