Search code examples
convertigo

Best method to send a complex json request with Convertigo


I have a complex json request that I'd like to POST to an api but I can't find the proper way to do this inside Convertigo Studio. Could anyone please indicate me the best way to do this?

Here is the request I'm send through curl that gives me the results. These result will be used by the front to display data.


curl -k -H "Accept: application/json" --compressed -XPOST https://myserverurl/api/search -d @- << EOF
{
"api-key":"somekey",
"usage":"someusage",
"criteria":{
    "timestamp":{"from-to":{"date-pattern":"yyyy/MM/dd-HHmmss","from":"2019/07/28-000000","to":"2019/08/27-235959"}},
    "timestamp-field":"timestamp",
    "metric":"*",
    "filter":{
                "and":
                [
                    {"eq":{"attribute":"type","value":"sometype"}},
                    {"simple-query":{"query":"_exists_:city"}},
                    {"neq":{"attribute":"status","value":"1"}}
                ]
            }
},
"info":"someinfo",
"size":10000,
"mode":"last-hits",
"format":{"tabular":{"columns":["col1", "col2","col3"],
"last-hits-columns":["name"],"order-by":[{"name":"name","direction":"ASC"}]},
"timestamp":{"date-pattern":"dd/MM/yyyy HH:mm:ss"}},
"index":"someindex",
"last-hits-count":"1"
}
EOF

I now would like to transpose that into a Convertigo approach using the proper connector and transactions but so far I'm hitting a wall. Any help is appreciated.

update: So I've managed to contact the API, i.e. reproducing the first part of the curl, by implementing a HTTP_Connector and then a HTTP_Transaction. The server is answering in the expected way.

Now what I can't do is posting the json string. I've tried implementing a http_single_variable which default value is that json string but it doesn't work, I get the following error:

HTTP result {ContentType: application/json, Length: 277}

{"error":{"request":"http://localhost:8550/api/search","message":"Unexpected character ('H' (code 72)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: java.io.InputStreamReader@6c195833; line: 1, column: 2]","target":"/search"}}

The error seems to be coming from the header, which has been defined as Accept, application/json. When I remove it I get a HTTP 500 Error from the server.


Solution

  • To post a JSON body in a Convertigo request you have to add a variable "__body" to your transaction:

    HTTP single-valued variable

    If your API returns a JSON response, you should use a JSON_HTTP_transaction instead of your HTTP_Transaction transaction.

    Set "HTTP verb" property transaction to POST and "HTTP headers" property to "Content-type, application/json".

    The value of the __body variable is set in a sequence by a Sequence_JS step like this:

    var data = {
       "param1": "value1",
       "param2": "value2",
       ...
       };
    

    Then, use a jElement step to transform "data" to a JSON string source:

    JSON.stringify(data)
    

    in "Expression" property.

    Next step is the Call of your transaction. In the __body Source point to the jElement text.

    Here is a link to a Convertigo (7.5.7+) sample: useBody.car

    Hope that Helps.