I want to create a Gist containing a JSON (valid one, I checked) with curl command as described here.
I tried first this script :
configText=$(cat jsonFile.json)
generate_post_data()
{
cat <<EOF
{
"description": "the description for this gist",
"public": true,
"files": {
"file1.txt": {
"content": $configText
}
}
}
EOF
}
curlBody="$(generate_post_data)"
curlResponse=$(curl -H "Content-Type: application/json" -X POST -d '$curlBody' https://api.github.com/gists)
Which gave me the error Problems parsing JSON, so I tried passing the file directly in the command:
curl -H "Content-Type:application/json" -data-binary #jsonFile.json https://api.github.com/gists
But I'm getting the same error. I know that this must be a conflict between the JSON body of the POST request and the JSON of my file (quotes, brackets...).
How can I send a clean JSON file to Gist ?
For the issues in your script :
in your curl request, you use single quotes around your bash variable in POST -d '$curlBody', use double quote to expand it : POST -d "$curlBody"
content is a text field : "content": $configText to "content": "$configText"
configText can have new lines and unescaped double quotes " which break your content JSON data. You could use the following to escape quotes and remove new lines :
configText=$(cat test.json | sed 's/\"/\\\"/g' | tr -d '\n')
The following example build your gist request with jq JSON parser/builder, not that this example will not preserve new lines in your input :
#!/bin/bash
ACCESS_TOKEN="YOUR_ACCESSS_TOKEN"
description="the description for this gist"
filename="file1.txt"
curlBody=$(jq --arg desc "$description" --arg filename "$filename" '.|
{ "description": $desc,
"public": true,
"files": {
($filename) : {
"content": tostring
}
}
}' jsonFile.json)
curl -v -H "Content-Type: application/json" \
-H "Authorization: Token $ACCESS_TOKEN" \
-X POST -d "$curlBody" https://api.github.com/gists
The following will preserve new lines in your json input by replacing new lines with \\n :
#!/bin/bash
ACCESS_TOKEN="YOUR_ACCESSS_TOKEN"
description="the description for this gist. There are also some quotes 'here' and \"here\" in that description"
public="true"
filename="file1.txt"
desc=$(echo "$description" | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
json=$(cat test.json | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
curl -v -H "Content-Type: text/json; charset=utf-8" \
-H "Authorization: Token $ACCESS_TOKEN" \
-X POST https://api.github.com/gists -d #- << EOF
{
"description": "$desc",
"public": "$public",
"files": {
"$filename" : {
"content": "$json"
}
}
}
EOF
Note that your access token must have the gist scope
Related
I'm trying to get an array from a json file.
{
"Requests": [
{
"Item1": "2020-01-27 16:24:49",
"Item2": "203i1Kj2gTEQgfdsfds23",
"Item3": 1603,
"Item4": "generic"
},
{
"Item1": "2020-01-27 16:24:49",
"Item2": "203i1Kj2gTEQgfdsfds23",
"Item3": 1603,
"Item4": "generic"
},
{
"Item1": "2020-01-27 16:24:49",
"Item2": "203i1Kj2gTEQgfdsfds23",
"Item3": 1603,
"Item4": "generic"
},
{
"Item1": "2020-01-27 16:24:49",
"Item2": "203i1Kj2gTEQgfdsfds23",
"Item3": 1603,
"Item4": "generic"
}
]
}
Then I want to pass each of these items to a curl request and possibly handle that in parralel with xargs
I'm still stumbling on getting one item to the curl endpoint.
cat CurlArgsFile.json | jq -c '.[][0]' | xargs -I % curl -d % -H "Content-Type: application/json" -X POST http://localhost:53391/mySVCS/writeData
I'm running this on Git Bash on Windows 10. When I try to echo the json output into a file I get the below without the quotes and now I'm not sure if that's the correct json I'm sending at all.
{Item1:2020-01-27 16:24:49,Item2:203i1Kj2gTEQgfdsfds23,Item3:1603,Item4:generic}
How do I send the first item from the json to the endpoint and the enpoint recognizes it?
This is the end result. The issue was I was not familiar with the jq utility and my json kept coming in wrong.
cat CurlArgsFile.json | jq -r '.Requests[]|tojson' | xargs -0 -I % curl -d % -H "Content-Type: application/json" -X POST http://localhost:53391/mySVC/writeData
If you use GNU xargs, the -d flag is usefule:
jq '.Requests[]' -rc file.json |
xargs -d '\n' -0 -P0 -I# curl -X POST -H 'Content-Type: application/json' -d # http://localhost:53391/mySVC/writeData
Otherwise, use tr to make it compatible with other xargs such as BSD or BusyBox.
jq '.Requests[]' -rc file.json |
tr '\n' '\0' |
xargs -0 -P0 -I# curl -X POST -H 'Content-Type: application/json' -d # http://localhost:53391/mySVC/writeData
So I'm writing a script that needs to create a json object, and post it with curl.
This is working:
curl --header "Content-Type: application/json" --request POST --data '{ "_type": "_test", "_device": "123.123.123.123", "_system": "web-services", "result": "success", "_time": "123", "error": "" }' $data_pipeline
$data_pipeline contains the URL for the post request
if $json_string contains the string including single quotes:
'{ "_type": "_test", "_device": "123.123.123.123", "_system": "web-services", "result": "success", "_time": "123", "error": "" }'
This should work but it doesn't:
curl --header "Content-Type: application/json" --request POST --data $json_string $data_pipeline
First I was creating the $json_object without the single quotes, and tried to add them on the command line for CURL. If I don't escape the single quotes, $json_string is sent as a literal, instead of expanding the variable. I escaped the single quotes, and it did not help, I even tried a double escape in case that was needed, and still it is not working. It only works if I put the entire json string by hand, but not if I put it in a variable. How can I fix this? The json is dynamically created by the script, using jq, and the json is valid as I can successfully run the post by hand, I just need it to work with a variable. It won't work without the single quotes, but the single quotes don't work when I use a variable to hold the json, it doesn't matter if I put the single quotes in the variable, or try to do it outside of the variable... How do I fix this?
The json object is built with this code:
json_string=\'$( jq -n \
--arg _type "$_type" \
--arg _device "$_device" \
--arg _system "$_system" \
--arg result "$result" \
--arg _time "$_time" \
--arg error "$error" \
'{_type: $_type, _device: $_device, _system: $_system, result: $result, _time: $_time, error: $error}' )\'
Originally I was creating the json_string without the ' but I added that in attempt to get the single quotes wrapped around the json.
Thanks!
#!/bin/bash
_type="hello"
# let's have some fun
_device="single'quote"
_system='double"quote'
error='multi
line'
encode()
{
printf "%s" "$1" | sed 's/"/%22/' | tr '\n' ' '
}
# you don't need encode() if your strings are not json-offensive
json_string="{
_type: \"$_type\"
_device: \"$_device\"
_system: \"$(encode "$_system")\"
error: \"$(encode "$error")\"
}"
set -x
curl \
-X POST \
-d "$json_string" \
-H 'content-type: application/json' \
http://httpbin.org/anything
Output:
+ curl -X POST -d '{
_type: "hello"
_device: "single'\''quote"
_system: "double%22quote"
error: "multi line"
}' -H 'content-type: application/json' http://httpbin.org/anything
{
"args": {},
"data": "{\n\t_type: \"hello\"\n\t_device: \"single'quote\"\n\t_system: \"double%22quote\"\n\terror: \"multi line\"\n}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "92",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0",
},
"json": null,
"method": "POST",
"url": "http://httpbin.org/anything"
}
I have a bash script that does a post request to a GraphQL endpoint. The post body is JSON. Basically within that JSON, I would like to replace a value of one key/value pair with a value from a bash variable. I've tried different combinations but all of them resulted to errors. Here's my code:
#!/bin/bash
utc_format=$(date -v-2d -v-23H -u +"%Y-%m-%dT%H:%M:%SZ")
curl -v \
-X POST \
-H "Content-Type: application/json" \
-H "X-Auth-Email: email#email.com" \
-H "X-Auth-key: $API_KEY" \
--data '{"query": "query { viewer { zones ( filter: { zoneTag_in: [ \"abcde12345\" ] } ) { firewallEventsAdaptive ( filter: { source: \"waf\" datetime_gt: "'"\"$utc_format\""'" ruleId: \"100000\" action: \"simulate\" } orderBy: [ datetime_DESC ] limit: 100 ) { clientIP edgeResponseStatus metadata { key value } } } } }"}' \
https://api.api.com/graphql \
| python -m json.tool >> curl_results
If you look at my code, the "' at the end of datetime_gt is to close the '{" in --data. Then before rule_id, I opened it up again with '". What am I doing wrong?? I probably checked this a hundred times. Every time I try a different formatting, a new error pops up. Currently (with the code pasted here), the error that I get is:
{
"data": null,
"errors": [
{
"extensions": {
"timestamp": "2020-12-21T03:32:11.090255834Z"
},
"message": "failed to recognize JSON request: 'invalid character '\"' after object key:value pair'",
"path": null
}
]
}
Check if it works. At least you gonna have a valid JSON as output.
#!/bin/bash
utc_format=$(date -v-2d -v-23H -u +"%Y-%m-%dT%H:%M:%SZ")
curl -v \
-X POST \
-H "Content-Type: application/json" \
-H "X-Auth-Email: email#email.com" \
-H "X-Auth-key: $API_KEY" \
--data '{"query": "query { viewer { zones ( filter: { zoneTag_in: [ \"abcde12345\" ] } ) { firewallEventsAdaptive ( filter: { source: \"waf\" datetime_gt: \"'$utc_format'\" ruleId: \"100000\" action: \"simulate\" } orderBy: [ datetime_DESC ] limit: 100 ) { clientIP edgeResponseStatus metadata { key value } } } } }"}' \
https://api.api.com/graphql \
| python -m json.tool >> curl_results
Is it possible to use the Global Instance of Keystone to retrieve registered user profile info?
According to these references: https://github.com/telefonicaid/fiware-pep-steelskin#keystone and Keystone create user and permissions by api, it seems possible if I wish to install an instance by my own. However, what if I wish to use the Global Instance, instead. Is it possible?
For example, I have tested te retrieve some data as indicated below without success:
curl -s -H "X-Auth-Token:cXylpiNyh74V6J9YOlqN2GTzYSmGQa" http://cloud.lab.fiware.org:4730/v2.0/tokens | python -mjson.tool
curl -s -H "X-Auth-Token:cXylpiNyh74V6J9YOlqN2GTzYSmGQa" http://cloud.lab.fiware.org:4730/v3/users/ | python -mjson.tool
curl http://cloud.lab.fiware.org:4730/v3/auth/tokens -H "Content-Type: application/json" -d ' { "auth": { "identity": { "methods": [ "password" ], "password": { "user": { "domain": { "name": "matest" }, "name": "pep_proxy_99c59...", "password": "e3025a286dab..." } } } } }'
Note: I have tried both port: 5000 and 4730.
Any hint will be appreciated.
Users doesn't have permissions to see other users information or to create new users using the API.
However, you can issue tokens from the global keystone using both v2.0 and v3 protocols:
curl -X POST http://130.206.84.8:4730/v2.0/tokens \
-H 'Content-Type: application/json' \
-d "{\"auth\": {\"tenantName\": \"${OS_TENANT_NAME}\",
\"passwordCredentials\": {
\"username\": \"$OS_USERNAME\",
\"password\": \"$OS_PASSWORD\"}}}" | \
jq -r '.access.token.id'
Or issue a token in v3:
curl -v -H "Content-Type: application/json" -d "
{ \"auth\": {
\"identity\": {
\"methods\": [\"password\"],
\"password\": {
\"user\": {
\"name\": \"$OS_USERNAME\",
\"domain\": { \"id\": \"default\" },
\"password\": \"$OS_PASSWORD\"
}
}
}
}
}" http://cloud.lab.fiware.org:4730/v3/auth/tokens 2>&1 \
| grep -i "X-Subject-Token"
There are few things you can do with Keystone itself using these tokens if you are not the admin user (Non admin users obviously have few permissions). However, you coud, for instance query the endpoints:
curl -s -H "X-Auth-Token: $TOKEN_ID" http://130.206.84.8:4730/v3/endpoints
The domains:
curl -s -H "X-Auth-Token: $TOKEN_ID" http://130.206.84.8:4730/v3/domains
I have the following bash script
#!/bin/bash
body=$(cat << EOF
{
"CreatedBy": "$(hostname -f)",
"Id": "$(uuidgen)",
"Type": "TestAlertType",
"AlertCategory": "NonUrgent"
}
EOF
)
curl -H "Content-Type: application/json" -X POST -d $body https://dev.cloudapp.net/v1/
But I get invalid json error on post. What am i missing?
This worked
curl -H "Content-Type: application/json" -X POST -d "$body" https://dev.cloudapp.net/v1/