How to store only one response of curl command [duplicate] - linux

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 1 year ago.
i want to store the output of curl command in a variable but not whole response only one value of that response
i have this curl command
curl -H "Authorization: Bearer $TOKEN" -X POST -H "content-type:application/json" \
-d '{
"name":"'"$ORG_NAME"'",
"displayName":"'"$ORG_DISPLAY_NAME"'",
"description":"'"$ORGANIZATION_DESCRIPTION"'",
"runtimeType":"'"$RUNTIMETYPE"'",
"analyticsRegion":"'"$ANALYTICS_REGION"'"
}' \
"https://apigee.googleapis.com/v1/organizations?parent=projects/$PROJECT_ID"
when i hit this command then it will show the response look like this
{
"name": "organizations/heloo/operations/keijfiejwfefekd",
"metadata": {
"#type": "type.googleapis.com/google.cloud.apigee.v1.OperationMetadata",
"operationType": "INSERT",
"targetResourceName": "organizations/heloo",
"state": "IN_PROGRESS"
}
}
so in the response i want to store this state value in a variable

test=$(curl -H "Authorization: Bearer $TOKEN" -X POST -H "content-type:application/json" \
-d '{
"name":"'"$ORG_NAME"'",
"displayName":"'"$ORG_DISPLAY_NAME"'",
"description":"'"$ORGANIZATION_DESCRIPTION"'",
"runtimeType":"'"$RUNTIMETYPE"'",
"analyticsRegion":"'"$ANALYTICS_REGION"'"
}' \
"https://apigee.googleapis.com/v1/organizations?parent=projects/$PROJECT_ID" | jq '.metadata.state')
it will work

Related

Send whatsapp message without template

Is there a way to send a whatsapp message without template? I'm using the graph.facebook.com/v15.0 API and couldn't find any info about it
I'm trying this
curl --location --request POST 'https://graph.facebook.com/v15.0/{phoneId}/messages' \
--header 'Authorization: Bearer Token' \
--header 'Content-Type: application/json' \
--data-raw '{
"messaging_product": "whatsapp",
"to": "anyNumber",
"text": {
"body": "hello world!"
}
}'
Here's the error:
I was using the wrong payload. Thanks to #turivishal.
https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-messages
I was also using the wrong Bearer Token.

how to use loop with some conditions in shell script

I need to run a curl command several times, and check the result every time. I used a for loop to check the output from curl, and I want to exit the loop when the correct status is reached.
The very first time the curl command is run, it will show me this output :
{
"name": "organizations/org_name/operations/long_running_operation_ID",
"metadata": {
"#type": "type.googleapis.com/google.cloud.apigee.v1.OperationMetadata",
"operationType": "INSERT",
"targetResourceName": "organizations/org_name",
"state": "IN_PROGRESS"
}
}
It takes time to complete, that's why it says IN_PROGRESS. It took around 30 to 60 sec, and if we run the same curl command after 30 to 60 sec then it will show the output below :
{
"error": {
"code": 409,
"message": "org graceful-path-310004 already associated with another project",
"status": "ALREADY_EXISTS",
"details": [
{
"#type": "type.googleapis.com/google.rpc.RequestInfo",
"requestId": "11430211642328568827"
}
]
}
}
Here is the script I have so far :
test=$(curl -H "Authorization: Bearer $TOKEN" -X POST -H "content-type:application/json" \
-d '{
"name":"'"$ORG_NAME"'",
"displayName":"'"$ORG_DISPLAY_NAME"'",
"description":"'"$ORGANIZATION_DESCRIPTION"'",
"runtimeType":"'"$RUNTIMETYPE"'",
"analyticsRegion":"'"$ANALYTICS_REGION"'"
}' \
"https://apigee.googleapis.com/v1/organizations?parent=projects/$PROJECT_ID" | jq '.error.status')
echo $test
while [ $test = "ALREADY EXISTS" || $test != "IN_PROGRESS" ]
do
echo $test
echo "Organization is creating"
test=$(curl -H "Authorization: Bearer $TOKEN" -X POST -H "content-type:application/json" \
-d '{
"name":"'"$ORG_NAME"'",
"displayName":"'"$ORG_DISPLAY_NAME"'",
"description":"'"$ORGANIZATION_DESCRIPTION"'",
"runtimeType":"'"$RUNTIMETYPE"'",
"analyticsRegion":"'"$ANALYTICS_REGION"'"
}' \
"https://apigee.googleapis.com/v1/organizations?parent=projects/$PROJECT_ID" | jq '.error.status')
done
echo "exit from loop"
To reduce the duplicated code, use functions.
Also, best practice to use jq to generate json -- it will safely handle any embedded quotes in the data:
json() {
jq --arg name "$ORG_NAME" \
--arg displayName "$ORG_DISPLAY_NAME" \
--arg description "$ORGANIZATION_DESCRIPTION" \
--arg runtimeType "$RUNTIMETYPE" \
--arg analyticsRegion "$ANALYTICS_REGION" \
--null-input --compact-output \
'$ARGS.named'
}
errorStatus() {
curl -H "Authorization: Bearer $TOKEN" \
-X POST \
-H "content-type:application/json" \
-d "$(json)" \
"${URL}?parent=projects/$PROJECT_ID" \
| jq -r '.error.status'
}
while true; do
status=$(errorStatus)
[ "$status" = "ALREADY EXISTS" ] && break
done
echo "exit from loop"
The while true; do some code; condition && break; done bit is the bash version of a do-while loop

How to post data to kobotoolbox form using the Kobo API?

I'm working with the kobo REST API and I can get the structure's information from the form and the submitted data through the API, but when I try to send data to the form through a POST service, kobo returns the following message: "Your data for instance None has been already submitted.".
Is there any way to send POST data to a kobotoolbox form using its API?
I tried to send the data with the following POST request:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \
"name": "Foo" \
}' 'https://kf.kobotoolbox.org/assets/{form_id}/submissions/'
Where {form_id} is the id from my form in kobotoolbox
And the response was:
{
"detail": "Your data for instance None has been already submitted."
}
To submit data you need to POST to https://[kobocat url]/api/v1/submissions, e.g. https://kc.kobotoolbox.org/api/v1/submissions. The endpoint you tried is for retrieving submitted data.
This will work (for a form that contains a first_name and last_name question:
curl -X POST \
https://kc.kobotoolbox.org/api/v1/submissions.json \
-H 'Authorization: Token <yourAPItoken>' \
-H 'Content-Type: application/json' \
-d ' {
"id": "<kpi asset ID>",
"submission":
{
"last_name": "Suresh",
"first_name":"Ameen",
"meta": {
"instanceID": "uuid:8992c3c2-3418-4ba9-a8e2-b00e03ea36b6"
}
}
} '
<kpi asset ID> in this case refers to the unique form ID created in kobocat. This ID should be the same as the kpi asset ID, as long as you uploaded/created your form in the regular interface and deployed it.
You can find the ID by going to https://kf.kobotoolbox.org/assets/<kpi asset ID>/. In my case I get this:
{
"url": "https://kf.kobotoolbox.org/assets/aCNoKPdCX8gqcu4bHrHkUL/",
...
"deployment__identifier": "https://kc.kobotoolbox.org/<username>/forms/aCNoKPdCX8gqcu4bHrHkUL",
...
"uid": "aCNoKPdCX8gqcu4bHrHkUL",
]
}

escape variable in bash script curl request

I am trying to curl via bash script but unable to pass the value of var1 in curl request and getting error upon execution ...
#!/bin/bash
var1="some test message"
curl 'https://link' \
-H 'Content-Type: application/json' \
-d '
{"msgtype": "text",
"text": {
"content": $var1
}
}'
Your variable $var1 doesn't get expanded by the shell because it is inside single quote '.
You need to use double quote to let bash do the parameter expansion, and escape your json data:
#!/bin/bash
var1="some test message"
curl 'https://link' \
-H 'Content-Type: application/json' \
-d "
{
\"msgtype\": \"text\",
\"text\": {
\"content\": \"$var1\"
}
}"
Or you can use inline document (without the escaping hell, but the command becomes awkward):
#!/bin/bash
var1="some test message"
curl 'https://link' \
-H 'Content-Type: application/json' \
-d "$(cat <<EOT
{
"msgtype": "text",
"text": {
"content": "$var1"
}
}
EOT
)"
In general, don't use parameter expansion to define JSON dynamically like this. There is no guarantee that your template, combined with the contents of the variable, produces well-formed JSON. (This is for the same reasons you don't use string interpolation to create dynamic SQL queries.) Use a tool like jq instead.
curl 'https://link' ... \
-d "$(jq --argjson x "$var" \
-n '{msgtype: "text", text: {content: $x}}')"
Use below script.
https://aaa.com' -H 'Content-Type: application/json' -d '{"msgtype": "text", "text": { "content": '"'$var1'"' }}'

Using Curl with Json (Node.js)

I am having trouble getting curl and Json (Node.js) to work when I in curl omit the "[ and ]" part of the JSON-RPC request params. I am using named parameters (i.e. an object, not an array).
This works:
curl -v -i POST -H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method":"registerPerson", "params": ["{\"username\":\"morten10\",\"password\":\"mypass\"}"], "id":1 }' \
http://localhost:3000
However, when I omit the [" and ]" from the params it doesn't work:
curl -v -i POST -H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method":"registerPerson", "params": {\"username\":\"morten10\",\"password\":\"mypass\"}, "id":1 }' \
http://localhost:3000
and Jayson gives this error message:
TypeError: First argument must be a string or Buffer
However, according to the JSON-RPC 2.0 specification and its examples, I should be able to exclude the "[ and ]" part from params when I use named parameters, as in this example:
--> {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}
<-- {"jsonrpc": "2.0", "result": 19, "id": 4}
What am I not getting?
Thanks!
I think your object needs to be a string as suggested by the error.
Try:
curl -v -i POST -H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method":"registerPerson", "params": "{\"username\":\"morten10\",\"password\":\"mypass\"}", "id":1 }' \
http://localhost:3000

Resources