打开REST密码交易

时间:2013-10-17 19:58:10

标签: rest groovy neo4j

尝试编写脚本以在groovy中打开事务。目前有:

def static transaction(statement, params,success, error)
{
    def http = new HTTPBuilder( 'http://localhost:7474' )
    http.request( POST, JSON ) {
        uri.path = '/db/data/transaction'
        headers.'X-Stream' = 'true'
        requestContentType = JSON
        body =  [ statements : statement , params : params ?: [:] ]

        // uri.query = [ param : 'value' ]

        response.success = { resp, json ->
            if (success) success(json)
            else {
                println "Status ${resp.statusLine} Columns ${json.columns}\nData: ${json.data}"
            }
        }

        response.failure = { resp, message ->
            def result=[status:resp.statusLine.statusCode,statusText:resp.statusLine.reasonPhrase]
            result.headers = resp.headers.collect { h -> [ (h.name) : h.value ] }
            result.message = message
            if (error) {
                error(result)
            } else {
                println "Status: ${result.status} : ${result.statusText} "
                println 'Headers: ${result.headers}'
                println 'Message: ${result.message}'
            }
        }
    }
}
transaction("start n=node(*) return n",[id:56981],{ println "Success: ${it}" },{ println "Error: ${it}" })

但是我在响应中收到以下错误:

Success: [commit:http://localhost:7474/db/data/transaction/4/commit, results:[], errors:[[code:40001, status:INVALID_REQUEST_FORMAT, message:Unable to deserialize request. Expected [START_OBJECT, FIELD_NAME, START_ARRAY], found [START_OBJECT, FIELD_NAME, VALUE_STRING].]]]

思考?谢谢!

2 个答案:

答案 0 :(得分:1)

请查看https://gist.github.com/7053223。基本上你没有正确构建json结构。根据{{​​3}},发送包含statements密钥的json映射。里面有一系列包含statementparameters键的地图。比较l.15和l.16-18,看看差异。

答案 1 :(得分:0)

面对与php相同的问题,statements必须包含statement的数组:

[
    statements => [
        statemant
    ],
    resultDataContents => [
        'row',
        'graph',
    ],
    'includeStats' => true,
]