curl with json array - json

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.

Related

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

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

Sending commands to devices in FIWARE Orion is not working

I am trying to provide a device on FIWARE and send commands. In my case, a lamp with commands ("on" and "off"). But, it's not working.
The steps to create a device and send command are:
Provide a device;
Create a service group;
Create a registration with commands;
Send a command.
I'm taking the fiware-tutorials following the steps, but don't work.
The code I have used was:
To create a device:
curl -iX POST \
'http://localhost:4041/iot/devices' \
-H 'Content-Type: application/json' \
-H 'fiware-service: openiot' \
-H 'fiware-servicepath: /' \
-d '{
"devices": [
{
"device_id": "lamp1",
"entity_name": "urn:ngsi-ld:Lamp:1",
"entity_type": "Lamp",
"protocol": "PDI-IoTA-UltraLight",
"transport": "HTTP",
"endpoint": "http://iot-sensors:3001/iot/lamp1",
"commands": [
{"name": "on","type": "command"},
{"name": "off","type": "command"}
],
"attributes": [
{"object_id": "s", "name": "state", "type":"Text"},
{"object_id": "l", "name": "luminosity", "type":"Integer"}
],
"static_attributes": [
{"name":"refStore", "type": "Relationship","value": "urn:ngsi-ld:Store:001"}
]
}
]
}
'
To create a service group:
curl -iX POST \
'http://localhost:4041/iot/services' \
-H 'Content-Type: application/json' \
-H 'fiware-service: openiot' \
-H 'fiware-servicepath: /' \
-d '{
"services": [
{
"apikey": "4jggokgpepnvsb2uv4s40d59ov",
"cbroker": "http://orion:1026",
"entity_type": "Thing",
"resource": "/iot/d"
}
]
}'
To create a registration for commands:
curl -iX POST \
'http://localhost:1026/v2/registrations' \
-H 'Content-Type: application/json' \
-H 'fiware-service: openiot' \
-H 'fiware-servicepath: /' \
-d '{
"description": "Lamp Commands",
"dataProvided": {
"entities": [
{
"id": "urn:ngsi-ld:Lamp:1","type": "Lamp"
}
],
"attrs": [ "on", "off" ]
},
"provider": {
"http": {"url": "http://orion:1026/v2"},
"legacyForwarding": true
}
}'
To send a "on" command:
curl -iX PATCH \
'http://localhost:1026/v2/entities/urn:ngsi-ld:Lamp:1/attrs' \
-H 'Content-Type: application/json' \
-H 'fiware-service: openiot' \
-H 'fiware-servicepath: /' \
-d '{
"on": {
"type" : "command",
"value" : ""
}
}'
Doing a HTTP get request to retrieve the entity:
curl -G -X GET \
'http://localhost:1026/v2/entities/urn:ngsi-ld:Lamp:1' \
-d 'type=Lamp' \
-d 'options=keyValues' \
-H 'fiware-service: openiot' \
-H 'fiware-servicepath: /'
I expect a output like:
{
"id": "urn:ngsi-ld:Lamp:001",
"type": "Lamp",
"TimeInstant": "2019-08-28T13:33:51.00Z",
"luminosity": "1115",
"off_info": " ",
"off_status": "UNKNOWN",
"on_info": " on OK",
"on_status": "OK",
"refStore": "urn:ngsi-ld:Store:001",
"state": "ON",
"on": "",
"off": ""
}
but I receive this:
{
"id": "urn:ngsi-ld:Lamp:1",
"type": "Lamp",
"TimeInstant": "2019-08-28T12:50:22.00Z",
"luminosity": " ",
"off_info": " ",
"off_status": "UNKNOWN",
"on_info": "There was an error in the response of a device to a command [404]: on NOT OK",
"on_status": "ERROR",
"refStore": "urn:ngsi-ld:Store:001",
"state": " ",
"on": "",
"off": ""
}
Assuming you are using the FIWARE Tutorial Dummy devices, then your provisioning steps are correct and the error message received (404) indicates that lamp1 is not responding to your command when sent.
When you are provisioning the device
"device_id": "lamp1",
"entity_name": "urn:ngsi-ld:Lamp:1",
"endpoint": "http://iot-sensors:3001/iot/lamp1",
You are stating that:
There exists a device (lamp1)
The device has an entity name in Orion (urn:ngsi-ld:Lamp:1)
The device is able to respond to commands on the url shown (http://iot-sensors:3001/iot/lamp1)
The final statement is incorrect.
The context-provider code sets up 4 dummy lamps:
lamp001
lamp002
lamp003
lamp004
But no lamp1 - therefore when an HTTP POST request is sent to http://iot-sensors:3001/iot/lamp1 the response is a 404 Not Found - which is correctly defined in your entity state in Orion.
BTW, #fgalan is entirely correct in stating that the registration is no longer necessary with more modern versions of the IoT Agents - it has been left in the tutorial purely for backwards compatibility with older versions.

CURL keep throwing "Invalid body format"

so I have this body of a CURL:
-d '{
"documentType": "SQMProjectFact",
"status": "published",
"selectAttributes": [
"Supplier"
],
"filterExpressions": [
{
"name": "createdDateFrom",
"field": "TimeCreated",
"op": ">=",
"defaultValue": "2018-08-05T01:01:59Z
" },
{
"name": "createdDateTo",
"field": "TimeCreated",
"op": "<=",
"defaultValue": "2018-08-15T01:01:59Z"
}
]
}'
When I run it on postman, it works tottaly fine, but when I try to do it on CMD, it keeps throwing me "Invalid body format".
I research and try chagning ' for ", and " for \", and a lot of conmbinations of that, erasing the spaces (just to see if it that solved it), but it stills throwing me that error.
Just for info, these are the headers:
-H "Authorization: Bearer xXxXXXxx-xxXx-xxxx-xxxx-xxxxxxxxxxxx"
-H "Content-Type: application/json"
-H "accept: application/json"
-H "apiKey: xxxxxxxxxxxxxxxxxxxxx"
Thanks for those who will reply!

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.

Why this orion subscription don't works as I want?

I have the following subscription in orion 1.2.1:
curl --include \
--header 'Content-Type: application/json' \
--request POST \
--data-binary '{
"description": "subscription",
"subject": {
"entities": [
{
"idPattern": "event-.*",
"type": "Event"
}
],
"condition": {
"attrs": [
"IdEvent",
"mFlag"
],
"expression": {
"q": "mFlag>0"
}
}
},
"notification": {
"attrsFormat":"legacy",
"http": {
"url" : "http://localhost:5050/notify"
},
"attrs": [
"IdEvent"
]
}
}' \
'http://localhost:1026/v2/subscriptions'
When I send an entity update like this one:
curl --include \
--request PATCH \
--header "Content-Type: application/json" \
--data-binary '{
"mFlag":{
"value":"5",
"type":"int"
}
}' \
'http://localhost:1026/v2/entities/event-2/attrs'
Orion is not notifiying and it is making me crazy don't know what is wrong. Any idea?
When I remove this part of the subscription:
"expression": {
"q": "mFlag>0"
}
it works but I need it to notify when any attribute is changed and the condition is satisfied.
Note that in NGSIv2 "5" is a string, not a number. Thus, in order to make the "q": "mFlag>0" filter works as expected, use the following update:
curl --include \
--request PATCH \
--header "Content-Type: application/json" \
--data-binary '{
"mFlag":{
"value":5,
"type":"int"
}
}' \
'http://localhost:1026/v2/entities/event-2/attrs'