Reputation: 1268
In nifi how to call POST json data and get json response. I have tried to use the HTTPbuilder by adding HTTPbuilder library. But getting exception
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.HttpResponseException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:677)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:787)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:775)
... 50 common frames omitted
Upvotes: 2
Views: 1351
Reputation: 2628
Below is the sample to to call POST rest service in GroovyScript
def jsonBody= '{\"query\":{\"bool\":{\"must\":[{\"match\":{\"FlowID\":' + flowID + '}}]}}}'
def url ="http://localhost/rest"
def post = new URL(url).openConnection()
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "application/json")
post.getOutputStream().write(jsonBody.getBytes("UTF-8"))
def w_getResponseCode = post.getResponseCode();
def response = ""
if (w_getResponseCode.equals(200)) {
response = post.getInputStream().getText()
}
//Parse response in json using JsonSlurper.
You can use the above code in ExecuteGroovyScript Processor or you can create json in ExecuteGroovyScript and then use InvokeHttp with POST http method
Upvotes: 3