【发布时间】:2018-04-10 23:21:08
【问题描述】:
我正在尝试编写一个 Bash 脚本,其中在本地服务器上托管的应用程序上触发了以下给定的 curl,我需要从 curl 响应中提取以下值并将它们存储在单独的变量中:
1) HTTP 响应代码,即。 201 2)ETag值:即。 292a74f6-6999-403c-bc3c-d78af9d98a2a 3)来自json响应的“sub”的值,即。 11deadd1-ca08-4995-aa4b-a980307f2b1d
`curl -X POST -v -sS http://localhost:8087/idp-backend/profile/ -H 'accept: application/json' -H 'content-type: application/json' -H 'x-int-opco-id: NV' -d '{"user_details": {
"name": "myname",
"given_name": "Diego",
"family_name": "Maradona",
"middle_name": "Armando",
"email": "test@gmail.com",
"email_verified": "true",
"gender": "male",
"phone_number": "491234569999",
"phone_number_verified": true,
"address": {
"street_address": "6767 Collins Ave",
"locality": "Miami Beach",
"region": "FL",
"postal_code": "33141",
"country": "DEU"
}
},
"device_details": {
"user_agent": "iOS",
"device_id": "123123123123123123",
"ip_address": "1.2.3.4"
},
"security_details": {
"password": "India@1234",
"security_questions": [{
"question": "Who is God?",
"answer": "me"
}]
}
}'
Response:
* About to connect() to localhost port 8087 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8087 (#0)
> POST /idp-backend/profile/ HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: localhost:8087
> accept: application/json
> content-type: application/json
> x-int-opco-id: NV
> Content-Length: 1471
> Expect: 100-continue
>
< HTTP/1.1 100
< HTTP/1.1 201
< Set-Cookie: JSESSIONID=AFB7012E00510A9627F5A6B49C0CB3E1; Path=/idp-backend; HttpOnly
< X-Application-Context: preprod-idp-crm
< ETag: 292a74f6-6999-403c-bc3c-d78af9d98a2a
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Mon, 09 Apr 2018 13:24:35 GMT
<
{"user_details":{"sub":"11deadd1-ca08-4995-aa4b-a980307f2b1d","name":"myname","given_name":"Diego","family_name":"Maradona","middle_name":"Armando","email":"shiv.mishra@vodafone.com","email_verified":true,"gender":"male","zoneinfo":"CET","locale":"AR","phone_number":"491234567893","phone_number_verified":true,"is_blocked":false,"updated_at":"2018-04-09T13:24:35Z","status":"active","market":"NV","address":{"street_address":"6767 Collins Ave","locality":"Miami Beach","region":"FL","postal_code":"33141","country":"de"}},"kyc_details":{"status":"not-started","last_updated":"2018-04-09T13:24:35Z","history":[{"status":"not-started","timestamp":"2018-04-09T13:24:35Z","comment":"User updated status with: not-started"}],"completeness":"partial","completeness_last_update":"2018-04-09T13:24:35Z"},"links":{"self":{"href":"http://localhost:8087/idp-backend/profile/11deadd1-ca08-4995-aa4b-a980307f2b1d"},"urn:vodafoneid:profile:update":{"href":"http://localhost:8087/idp-backend/profile/11deadd1-ca08-4995-aa4b-a980307f2b1d","method":"PATCH","data":{"name":"{name}","given_name":"{given_name}","family_name":"{family_name}","middle_name":"{middle_name}","preferred_username":"{preferred_username}","nickname":"{nickname}","profile":"{profile_url}","picture":"{picture_url}","website":"{website_url}","email":"{email_address}","email_verified":false,"gender":"{gender}","birthdate":"{birthdate}","birthplace":"{birthplace}","zoneinfo":"{zoneinfo}","locale":"{locale}","phone_number":"{phone_number}","phone_number_verified":true,"is_blocked":false,"status":"<active|inactive>","address":{"street_address":"{street_address}","locality":"{locality}","region":"{region}","postal_code":"{postal_code}","country":"{country}"},"vodafone_phone_number":false},"headers":[{"x-int-opco-id":"NV","Content-Type":"application/json"}]},"urn:vodafoneid:profile:delete":{"href":"http://localho* Connection #0 to host localhost left intact
* Closing connection #0
st:8087/idp-backend/profile/11deadd1-ca08-4995-aa4b-a980307f2b1d","method":"DELETE","headers":[{"x-int-opco-id":"NV"}]}}}
` 我正在尝试将 curl 响应存储在名为 content 的变量中并尝试执行 echo $content | jq -r '.[] .sub.
但是,这是行不通的,而且即使是 HTTP 响应和 Etag 值也不是 Json Body 的一部分,所以我如何编写 bash 脚本以便能够存储上述所有 3 个值(http , Etag & sub) 一次拍摄不同的变量。
提前致谢。
【问题讨论】:
-
jq过滤器应该是.user_details.sub。 -
感谢它为我工作。我也使用 -r 选项来删除双引号。你能告诉我们如何从以下 json 响应中检索子:
-
感谢它为我工作。我也使用 -r 选项来删除双引号。你能告诉我们如何从以下 json 响应中检索带有 jq 的子:{"links":{"self":{"href":"localhost:8087/idp-backend/profile/…"}},"offset":0,"count" :10,"totalItems":1,"items":[{"sub":"11deadd1-ca08-4995-aa4b-a980307f2b1d","name":"testuser","given_name":"Diego","family_name" :"Maradona","middle_name":"Armando","nickname":"masterofuniverse","phone_number":"491234567893","status":"active","country":"de"}}]}跨度>