curl: (3) [globbing] unmatched brace at pos 2 - json

I keep receiving this error : curl: (3) [globbing] unmatched brace at pos 2
and I don't even understand where is "pos 2"
Although strangely I don't have the same problem when I do that from bash. I run that code in Jenkins pipeline. That is why you see sh '''' wrapper
sh '''
curl -u ${GIT_USERNAME}:${GIT_PASSWORD} -H "Content-Type: application/json" -X POST https://tools.company.my.com/bitbucket/rest/build-status/1.0/commits/$GIT_COMMIT --data-binary #- <<BODY \
{
"state": "SUCCESSFUL",
"key": "$JOB_BASE_NAME",
"name": "$BUILD_TAG",
"url": "$BUILD_URL",
"description": "change"
}
BODY
'''

Assuming there is no single quote in ${GIT_PASSWORD} :
sh -c "curl -u '${GIT_USERNAME}:${GIT_PASSWORD}'\
-H 'Content-Type: application/json'\
-X POST 'https://tools.company.my.com/bitbucket/rest/build-status/1.0/commits/$GIT_COMMIT'\
--data-binary #-" << BODY
{
"state": "SUCCESSFUL",
"key": "$JOB_BASE_NAME",
"name": "$BUILD_TAG",
"url": "$BUILD_URL",
"description": "change"
}
BODY

Related

Having problems using curl post a json object from a variable in bash

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"
}

Uploading linked Revit models to Autodesk Forge

I've been trying to use the Post references (https://developer.autodesk.com/en/docs/model-derivative/v2/reference/http/urn-references-POST/) to set up the reference between two files in forge but although I get a message "success" as result when I try it on the forge viewer I still see the files separately even after I translate the models. Has someone been through the same issue?
Without seeing you code it is hard to tell what is happening. Below I copied my bash script code which references/translate an obj with material and texture.
Au.obj
+- Au.mtl
+- Au.jpg
After upload, I got these
idObj="urn:adsk.objects:os.object:cyrillejcrja/Au.obj"
idMtl="urn:adsk.objects:os.object:cyrillejcrja/Au.mtl"
idJpg="urn:adsk.objects:os.object:cyrillejcrja/Au.jpg"
the code to set references, now
urn=$(xbase64encode $idObj)
job='{
"urn": "'${idObj}'",
"filename": "Au.obj",
"references": [{
"urn": "'${idMtl}'",
"relativePath": "./Au.mtl",
"filename": "Au.mtl",
"references": [{
"urn": "'${idJpg}'",
"relativePath": "./Au.jpg"
}]
}]
}'
response=$(curl -H "Content-Type: application/json" \
-H "Authorization: ${bearer}" \
-X POST ${ForgeHost}/modelderivative/v2/designdata/${urn}/references \
-k -s -d "${job}")
Here is got a reply like below which only means that the references are registered.
{
"result": "success"
}
Now, I do this to translate the obj and use the references
urn=$(xbase64encode $idObj)
job='{
"input": {
"urn": "'${urn}'",
"checkReferences": true
},
"output": {
"formats": [
{
"type": "svf",
"views": [
"2d",
"3d"
]
}
]
}
}'
response=$(curl -H "Content-Type: application/json" \
-H "Authorization: ${bearer}" \
-H "x-ads-force: true" \
-X POST ${ForgeHost}/modelderivative/v2/designdata/job \
-k -s -d "${job}")
Note the "checkReferences": true, as documented here.
Now, I can wait the translation to complete and see the result in the Viewer.
For reference the xbase64safeencode function used above
function xbase64safeencode () { local id64=$(echo -ne $1 | base64 $wrap_arg | tr -d '=' | tr '+/' '-_'); echo $id64; }
#Cyrille, this is my request:
curl -X 'POST' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsI' -H 'Content-Type: application/json' -v 'https://developer.api.autodesk.com/modelderivative/v2/designdata/{urn}/references' -d
'{
"urn": "urn:adsk.objects:os.object:bucket/non-existent.rvt",
"filename": "",
"references": [
{
"urn": "urn:adsk.objects:os.object:bucket/non-existent.rvt",
"relativePath": "",
"filename": ""
}
]
}'
I got as result:
'{
"result": "success"
}'
The point is I'm getting success as a result even when I do not have the specified file on the server, so I'd suggest few server-side validations, for example, when a model has been translated once we can't set as reference right so it should at least return an error. Thank you and I hope this helps.

curl with json array

The first command works and the second doesn't. What do I need to change to allow the json array to work? Thank you in advance.
command #1:
curl -d '{"uid":"TEST", "object":"TEST"}' \
-H "Content-Type: application/json" \
-X POST http://WEBSERVER/api/v1/inventory
command #2
curl -d '{"uid":"TEST","object":["server1", "server2", "server3"]}' \
-H "Content-Type: application/json" \
-X POST http://WEBSERVER/api/v1/inventory
Your curl syntax is fine, you could test by using httpbin.org, for example:
$ curl -d '{"uid":"TEST","object":["server1", "server2", "server3"]}' \
-H "Content-Type: application/json" \
-X POST http://httpbin.org/post
Returns:
{
"args": {},
"data": "{\"uid\":\"TEST\",\"object\":[\"server1\", \"server2\", \"server3\"]}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Connection": "close",
"Content-Length": "57",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.54.0"
},
"json": {
"object": [
"server1",
"server2",
"server3"
],
"uid": "TEST"
},
"origin": "x.x.x.x",
"url": "http://httpbin.org/post"
}
You indeed could pipe the result and verify the output with your input:
$ curl -d '{"uid":"TEST","object":["server1", "server2", "server3"]}' \
-H "Content-Type: application/json" \
-X POST http://httpbin.org/post -s | jq -r '.data'
It will print:
{"uid":"TEST","object":["server1", "server2", "server3"]}
Probably the server you are using to post data don't accept your request, check the returned status code, could give a clue, maybe is a 400 (bad request) or a 406 (not acceptable) etc, just in case here is a list of possible status codes.

curl: (3) [globbing] bad range specification in column 3

Im trying to index a simple json data in solr using curl. When i use the command, it says
"curl -X POST -H 'Content-Type:application/json'-d http://localhost:8983/solr/informationretrieval/update/json/docs '[{"id":"1","title":"Doc 1"},{"id":"2","title":"Doc 2"}]'
{"responseHeader":{"status":0,"QTime":1}}
curl: (3) [globbing] bad range specification in column 3"
I have tried removing quotes, tried -g and --globoff etc but every time there are errors like illegal port number. Can anyone please help?
Adding Multiple JSON Documents
curl -X POST -H 'Content-Type: application/json' 'http://localhost:8983/solr/informationretrieval/update' --data-binary '
[
{
"id": "1",
"title": "Doc 1"
},
{
"id": "2",
"title": "Doc 2"
}
]'

Create JSON Gist (conflict)

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