This question already has answers here:
Passing a shell variable to a JSON request to curl?
(3 answers)
Closed 5 years ago.
My goal is to have an attribute isAdmin in my users schema that is a boolean value, defaulted to false, and can be sent a true value in a CURL request.
I started off with this in my schema:
isAdmin: {
type: Boolean,
default: false,
required: true
}
and have a working CURL request and the isAdmin returns false as expected.
API="http://localhost:4741"
URL_PATH="/sign-up"
EMAIL="a2#admin.com"
PASSWORD="test"
curl "${API}${URL_PATH}" \
--include \
--request POST \
--header "Content-Type: application/json" \
--data '{
"credentials": {
"email": "'"${EMAIL}"'",
"password": "'"${PASSWORD}"'",
"password_confirmation": "'"${PASSWORD}"'"
}
}'
I then tried to take the default value out:
isAdmin: {
type: Boolean,
required: true
}
..and try passing T or F in the CURL request and it's not working. The error says that isAdmin is required. Not sure what is wrong with my CURL request.
API="http://localhost:4741"
URL_PATH="/sign-up"
EMAIL="a2#admin.com"
PASSWORD="test"
ADMIN="True"
curl "${API}${URL_PATH}" \
--include \
--request POST \
--header "Content-Type: application/json" \
--data '{
"credentials": {
"email": "'"${EMAIL}"'",
"isAdmin": "'"${ADMIN}"'",
"password": "'"${PASSWORD}"'",
"password_confirmation": "'"${PASSWORD}"'"
}
}'
Any ideas as to what I am doing wrong? And/Or how I can have a default value of false and POST a new user with a true isAdmin attribute? Thank you in advance for any advice.
Clarification: Using Mongoose and ExpressAPI.
Since you are enclosing the value in double quotes, it seems to be treated as a string, rather than boolean. Try this:
ADMIN=true # may be it is safer to use all lowercase for true/false
curl "${API}${URL_PATH}" \
--include \
--request POST \
--header "Content-Type: application/json" \
--data '{
"credentials": {
"email": "'"${EMAIL}"'",
"isAdmin": '${ADMIN}',
"password": "'"${PASSWORD}"'",
"password_confirmation": "'"${PASSWORD}"'"
}
}'
Related
How can I change "Assigned To" from specific user to unspecified? I used {name: '-'} mainly for my UI which is obviously not enough to be used as input, perhaps {id: '', name: '-'} or {id: null, name: '-'}?
Update 2023-Jun
BIM360 issues API just released v2 API. Now it supports filtering assignedTo, assignedToType and displayId (in v1 called identifier). See here for the announcement: https://aps.autodesk.com/blog/bim-360-issues-version-2-api-released-1
Find the unassigned issues
curl --location --request GET 'https://developer.api.autodesk.com/issues/v2/containers/:issueContainerId/issues?filter[assignedTo]=null&filter[assignedToType]=null' \
--header 'Authorization: Bearer '
Find an issue by its displayId (in v1 called identifier)
curl --location --request GET 'https://developer.api.autodesk.com/issues/v2/containers/:issueContainerId/issues?filter[displayId]=14' \
--header 'Authorization: Bearer '
=========
We can specify null values to both assigned_to and assigned_to_type to remove the user from the issue and make it be unspecified.
curl --location --request PATCH 'https://developer.api.autodesk.com/issues/v1/containers/:issueContainerId/quality-issues/:issueId' \
--header 'Authorization: Bearer ' \
--header 'Content-Type: application/vnd.api+json' \
--data-raw '{
"data": {
"type": "quality_issues",
"id": "{{issueId}}",
"attributes": {
"assigned_to": null,
"assigned_to_type": null
}
}
}'
API ref: https://forge.autodesk.com/en/docs/bim360/v1/reference/http/field-issues-:id-PATCH/
The following script of curl command works fine with variables inside double-quoted string, but how do I use variables (e.g. ip_address and user_id) inside the --data-raw '{...}'?
#!/bin/sh
ip_address="10.0.0.1"
user_id="my-user-id"
websec_token="some_string"
version="v12"
curl -w #curl-format.txt \
--request POST "http://example.com/$version/web" \
--header "Authorization: Bearer $websec_token" \
--header "Content-Type: application/json" \
--data-raw '{
"ipAddress": "10.0.0.1",
"userId": "my-user-id"
}'
Just escape the double quotes:
--data-raw "{
\"ipAddress\": \"$ip_address\",
\"userId\": \"$user_id\"
}"
just add single quote arround the var part is better, as less \ introduced :
--data-raw '{
"ipAddress": "'$ip_address'",
"userId": "'$user_id'"
}'
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'm working with Orion Contex Broker and I need to receive notifications when a parameter in a structured attribute changes its value. An example:
Subscription:
curl -iX POST \
--url 'http://localhost:1026/v2/subscriptions' \
--header 'content-type: application/json' \
--data '{
"description":"Notify me of Store changes in street Address",
"subject":{
"entities":[
{
"idPattern":".*",
"type":"Store"
}
],
"condition":{
"attrs":[
"address.streetAddress"
]
}
},
"notification":{
"http":{
"url":"http://localhost:3000/subscription/store-change"
}
}
}'
Create entity:
curl -iX POST \
--url 'http://localhost:1026/v2/op/update' \
-H 'Content-Type: application/json' \
-d '{
"actionType":"append",
"entities":[
{
"type":"Store",
"id":"urn:ngsi-ld:Store:001",
"address":{
"type":"PostalAddress",
"value":{
"streetAddress":"Old",
"addressRegion":"Berlin"
}
},
"name":{
"type":"Text",
"value":"Bösebrücke Einkauf"
}
}
]
}'
Update the entity:
curl -iX PATCH \
--url 'http://localhost:1026/v2/entities/urn:ngsi-ld:Store:001/attrs' \
-H 'Content-Type: application/json' \
-d '{
"address":{
"type":"PostalAddress",
"value":{
"streetAddress":"Bornholmer"
}
}
}'
The expected result would be to receive a notification when the entity was created and update. Another possibility could be the "condition expressions". However one of kind: "q": "address.streetAddress!=${previousValue}" is not implemented yet.
Attributes within NGSI are usually numbers or strings - this typically leads to a very flat data model. In this case when the attribute value changes the subscription would be fired.
JSON objects (such as address above) are also supported, but the change occurs whenever the Object's value change and is not specifically bound to a sub attribute Hence
"attrs":[
"address.streetAddress"
]
Would need to be:
"attrs":[
"address"
]
However, the q parameter could be used to filter against a specific sub-attribute e.g. q=address.streetAddress!="Old" - and the listening interface could amend the subscription after it has fired.
I need to call an API where I increment the user ID every time, I have the following in the bash script, but keep getting a Unexpected token ' in JSON at position 2 error. What am I doing wrong?
for ((i=1;i<=5;i++)); do
curl -X POST --header 'Content-Type: application/json' -d "{ 'id': 'person'$i, 'name':
'person', 'info': {} }" 'http://localhost:9999/add'
It is a quoting issue. It is standard for JSON to have double quotes, try this
for ((i=1;i<=5;i++)); do
echo "Adding person"$i
curl -X POST --header 'Content-Type: application/json' --header
'Accept: application/json' --user 'admin' -d '{ "id": "person'$i'", "name":
"person", "info": {} }" 'http://localhost:9999/add'
done
You can use jq, to edit json by shellscript. See this link.