Search code examples
jsoncurldevopseclipse-ditto

Eclipse Ditto - Create a MQTT Connection - fails with invalid json 400 response


Establishing a connection to an MQTT 3.1.1 endpoint following the description here and the Operating Devops Commands end up in an invalid json 400 response. Even the example MQTT-Bidirectional gets refused with a 400. So this is why i am posting this question here to get hints what i am currently doing wrong and what i can do to get it right to help others running in the same issue. Here is my curl request:

    gd@gd:~/ditto/mosquitto$ curl -X POST 'http://{ditto-url}/devops/piggyback/connectivity?timeout=10000' -u devops:foobar -H 'Content-Type: application/json' -d createMQTT_connection.json
{"status":400,"error":"json.invalid","message":"Failed to parse JSON string 'createMQTT_connection.json'!","description":"Check if the JSON was valid (e.g. on https://jsonlint.com) and if it was in required format."}

The hint to check my json file gives back that my json is valid.

Here is how my json file currently looks like:

{

"targetActorSelection": "/system/sharding/connection",
"headers": {
"aggregate": false
},
"piggybackCommand": {
    "type": "connectivity.commands:createConnection",
    "connection": {
          "id": "mqtt-example-connection-123",
          "name": "mmqtt-example-connection-123",
          "connectionType": "mqtt",
          "connectionStatus": "open",
          "failoverEnabled": true,
          "uri": "tcp://{mqtt-broker-url}:1883",
          "sources": [
            {
              "addresses": [
            "{ditto-url}/#"
              ],
              "authorizationContext": ["nginx:ditto"],
              "qos": 0,
              "filters": []
            }
          ],
          "targets": [
            {
              "address": "{ditto-url}/{{ thing:id }}",
              "topics": [
            "_/_/things/twin/events"
              ],
              "authorizationContext": ["nginx:ditto"],
              "qos": 0
            }
          ]
        }
     }
}

Someone got an idea why this json is not valid?

Thanks for the support and hints to solve this issue!

[EDIT] First of all a "-f" makes more sense for the curl request:

curl -X POST -u devops:foobar 'http://{ditto-url}:8080/devops/piggyback/connectivity?timeout=10000' -f createMQTT_connection_1.json
curl: (22) The requested URL returned error: 400 Bad Request
curl: (6) Could not resolve host: createMQTT_connection_1.json

Second here the update json (with the result shown above)

{
    "targetActorSelection": "/system/sharding/connection",
    "headers": {
        "aggregate": false
    },
    "piggybackCommand": {
        "type": "connectivity.commands:createConnection",
        "connection": {
            "id": "mqtt-example-connection-123",
            "connectionType": "mqtt",
            "connectionStatus": "open",
            "failoverEnabled": true,
            "uri": "tcp://{MQTT-Broker-url}:1883",
            "sources": [{
                "addresses": ["ditto-tutorial/#"],
                "authorizationContext": ["nginx:ditto"],
                "qos": 0,
                "filters": []
            }],
            "targets": [{
                "address": "ditto-tutorial/{{ thing:id }}",
                "topics": [
                "_/_/things/twin/events",
                "_/_/things/live/messages"
                ],
                "authorizationContext": ["nginx:ditto"],
                "qos": 0
            }]
        }
    }
}

Solution

  • I think the problem you face is curl related. Please have a look here on how to send json data from a file: https://stackoverflow.com/a/18614411/5058051

    Seems the @ is missing in your case when specifying the file location.