java - Groovy HTTP ResponseParseException not found -
i trying make simple get
request using groovy http request. expecting json response in form:
{records:[]}
so far have done in code:
def getcountries(def cookies) { if (!cookies) { println("cookies list empty!") return false } println("get countries, cookies list: ") println(cookies) try{ def http = new httpbuilder("https://www.evisitor.hr/test_api/rest/") http.request(get, contenttype.json) { uri.path = "htz/country/recordsandtotalcount?psize=20&page=1&sort=namenational%20desc" headers.accept = 'application/json' headers.'user-agent' = 'mozilla/5.0 ubuntu/8.10 firefox/3.0.4' string c = cookies.join(";") println("before appending in header: ") println(c) headers.'set-cookie' = c response.success = { respnse, reader -> string text = resp.entity.content.text println("text is: " + text) string contenttype = resp.headers."content-type" println("content type is: " + contenttype) if (contenttype?.startswith("application/json")) { def json = jsonslurper().parsetext(text) println(json) println("1") } else { println("2") } } response.failure = { resp, reader -> println "failure response: ${resp.status}" } } }catch (groovyx.net.http.httpresponseexception ex) { println("3") ex.printstacktrace() return null }catch (java.net.connectexception ex) { println("4") ex.printstacktrace() return null } }
and getting response:
3 | error groovyx.net.http.responseparseexception: not found | error @ groovyx.net.http.httpbuilder.dorequest(httpbuilder.java:500) | error @ groovyx.net.http.httpbuilder.dorequest(httpbuilder.java:441) | error @ groovyx.net.http.httpbuilder.request(httpbuilder.java:390) | error @ groovyx.net.http.httpbuilder$request$0.call(unknown source) | error @ org.codehaus.groovy.runtime.callsite.callsitearray.defaultcall(callsitearray.java:45) | error @ org.codehaus.groovy.runtime.callsite.abstractcallsite.call(abstractcallsite.java:108) | error @ org.codehaus.groovy.runtime.callsite.abstractcallsite.call(abstractcallsite.java:124) | error @ com.manas.evisitor.evisitorservice$$eppve4fg.$tt__getcountries(evisitorservice.groovy:175) | error @ com.manas.evisitor.evisitorservice$$dppve4fg.$tt__getcountries(unknown source) | error @ com.manas.evisitor.evisitorservice.$tt__getcountries(evisitorservice.groovy) | error @ com.manas.evisitor.evisitorservice$_getcountries_closure3$$eppve4fg.docall(evisitorservice.groovy) | error @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) | error @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) | error @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) | error @ java.lang.reflect.method.invoke(method.java:606) | error @ org.springsource.loaded.ri.reloadedtypeinvoker$2.invoke(reloadedtypeinvoker.java:122) | error @ org.springsource.loaded.ri.reflectiveinterceptor.jlrmethodinvoke(reflectiveinterceptor.java:1299) | error @ org.codehaus.groovy.reflection.cachedmethod.invoke(cachedmethod.java:90) | error @ groovy.lang.metamethod.domethodinvoke(metamethod.java:324) | error @ groovy.lang.metaclassimpl.invokemethod(metaclassimpl.java:1207) | error @ groovy.lang.expandometaclass.invokemethod(expandometaclass.java:1110) | error @ groovy.lang.metaclassimpl.invokemethod(metaclassimpl.java:1016) | error @ groovy.lang.closure.call(closure.java:423) | error @ com.manas.evisitor.evisitorservice$_getcountries_closure3.call(evisitorservice.groovy) | error @ groovy.lang.closure.call(closure.java:439) | error @ com.manas.evisitor.evisitorservice$_getcountries_closure3.call(evisitorservice.groovy) | error @ org.codehaus.groovy.grails.orm.support.grailstransactiontemplate$2.dointransaction(grailstransactiontemplate.groovy:88) | error @ org.springframework.transaction.support.transactiontemplate.execute(transactiontemplate.java:133) | error @ org.codehaus.groovy.grails.orm.support.grailstransactiontemplate.execute(grailstransactiontemplate.groovy:85) | error @ com.manas.evisitor.evisitorservice$$eppve4fg.getcountries(evisitorservice.groovy) | error @ com.manas.evisitor.evisitorservice$$dppve4fg.getcountries(unknown source) | error @ com.manas.evisitor.evisitorservice.getcountries(evisitorservice.groovy) | error @ com.manas.evisitor.evisitorservice$getcountries$0.call(unknown source) | error @ org.codehaus.groovy.runtime.callsite.callsitearray.defaultcall(callsitearray.java:45) | error @ com.manas.evisitor.evisitorservice$getcountries$0.call(unknown source) | error @ com.manas.frontdesk.evisitorcontroller.testevisitor(evisitorcontroller.groovy:42) | error @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) | error @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) | error @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) | error @ java.lang.reflect.method.invoke(method.java:606) | error @ org.springsource.loaded.ri.originalclassinvoker.invoke(originalclassinvoker.java:47) | error @ org.springsource.loaded.ri.reflectiveinterceptor.jlrmethodinvoke(reflectiveinterceptor.java:1299) | error @ org.codehaus.groovy.grails.web.servlet.mvc.mixedgrailscontrollerhelper.invoke(mixedgrailscontrollerhelper.java:154) | error @ org.codehaus.groovy.grails.web.servlet.mvc.abstractgrailscontrollerhelper.handleaction(abstractgrailscontrollerhelper.java:375) | error @ org.codehaus.groovy.grails.web.servlet.mvc.abstractgrailscontrollerhelper.executeaction(abstractgrailscontrollerhelper.java:252) | error @ org.codehaus.groovy.grails.web.servlet.mvc.abstractgrailscontrollerhelper.handleuri(abstractgrailscontrollerhelper.java:205) | error @ org.codehaus.groovy.grails.web.servlet.mvc.abstractgrailscontrollerhelper.handleuri(abstractgrailscontrollerhelper.java:126) | error @ org.codehaus.groovy.grails.web.servlet.mvc.simplegrailscontroller.handlerequest(simplegrailscontroller.java:72) | error @ org.springframework.web.servlet.mvc.simplecontrollerhandleradapter.handle(simplecontrollerhandleradapter.java:50) | error @ org.codehaus.groovy.grails.web.servlet.grailsdispatcherservlet.dodispatch(grailsdispatcherservlet.java:347) | error @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:870) | error @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:961) | error @ org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:852) | error @ javax.servlet.http.httpservlet.service(httpservlet.java:620) | error @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:837) | error @ javax.servlet.http.httpservlet.service(httpservlet.java:727) | error @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:303) | error @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) | error @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) | error @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) | error @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) | error @ grails.plugin.cache.web.filter.pagefragmentcachingfilter.dofilter(pagefragmentcachingfilter.java:198) | error @ grails.plugin.cache.web.filter.abstractfilter.dofilter(abstractfilter.java:63) | error @ org.springframework.web.filter.delegatingfilterproxy.invokedelegate(delegatingfilterproxy.java:344) | error @ org.springframework.web.filter.delegatingfilterproxy.dofilter(delegatingfilterproxy.java:261) | error @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) | error @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) | error @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:101) | error @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) | error @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) | error @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:101) | error @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) | error @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) | error @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:101) | error @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) | error @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) | error @ org.apache.catalina.core.applicationdispatcher.invoke(applicationdispatcher.java:748) | error @ org.apache.catalina.core.applicationdispatcher.processrequest(applicationdispatcher.java:486) | error @ org.apache.catalina.core.applicationdispatcher.doforward(applicationdispatcher.java:411) | error @ org.apache.catalina.core.applicationdispatcher.forward(applicationdispatcher.java:338) | error @ org.codehaus.groovy.grails.web.mapping.urlmappingutils.forwardrequestforurlmappinginfo(urlmappingutils.java:178) | error @ org.codehaus.groovy.grails.web.mapping.urlmappingutils.forwardrequestforurlmappinginfo(urlmappingutils.java:144) | error @ org.codehaus.groovy.grails.web.mapping.urlmappingutils.forwardrequestforurlmappinginfo(urlmappingutils.java:135) | error @ org.codehaus.groovy.grails.web.mapping.filter.urlmappingsfilter.dofilterinternal(urlmappingsfilter.java:216) | error @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) | error @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) | error @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) | error @ org.codehaus.groovy.grails.web.servlet.mvc.grailswebrequestfilter.dofilterinternal(grailswebrequestfilter.java:69) | error @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) | error @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) | error @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) | error @ org.codehaus.groovy.grails.web.filters.hiddenhttpmethodfilter.dofilterinternal(hiddenhttpmethodfilter.java:67) | error @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) | error @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) | error @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) | error @ org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:88) | error @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) | error @ org.springframework.web.filter.delegatingfilterproxy.invokedelegate(delegatingfilterproxy.java:344) | error @ org.springframework.web.filter.delegatingfilterproxy.dofilter(delegatingfilterproxy.java:261) | error @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) | error @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) | error @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:220) | error @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:122) | error @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) | error @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103) | error @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116) | error @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) | error @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1070) | error @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:611) | error @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:314) | error @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) | error @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) | error @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) | error @ java.lang.thread.run(thread.java:745) | error caused by: groovy.json.jsonexception: unable determine current character, not string, number, array, or object current character read '<' int value of 60 unable determine current character, not string, number, array, or object line number 1 index number 0 <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> ^ | error @ groovy.json.internal.jsonparserchararray.decodevalueinternal(jsonparserchararray.java:216) | error @ groovy.json.internal.jsonparserchararray.decodevalue(jsonparserchararray.java:166) | error @ groovy.json.internal.jsonparserchararray.decodefromchars(jsonparserchararray.java:45) | error @ groovy.json.internal.jsonparserchararray.parse(jsonparserchararray.java:409) | error @ groovy.json.internal.basejsonparser.parse(basejsonparser.java:121) | error @ groovy.json.jsonslurper.parse(jsonslurper.java:224) | error @ groovyx.net.http.parserregistry.parsejson(parserregistry.java:280) | error @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) | error @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) | error @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) | error @ java.lang.reflect.method.invoke(method.java:606) | error @ org.springsource.loaded.ri.reflectiveinterceptor.jlrmethodinvoke(reflectiveinterceptor.java:1276) | error @ org.codehaus.groovy.reflection.cachedmethod.invoke(cachedmethod.java:90) | error @ groovy.lang.metamethod.domethodinvoke(metamethod.java:324) | error @ groovy.lang.metaclassimpl.invokemethod(metaclassimpl.java:1207) | error @ groovy.lang.expandometaclass.invokemethod(expandometaclass.java:1110) | error @ groovy.lang.metaclassimpl.invokemethod(metaclassimpl.java:1074) | error @ groovy.lang.expandometaclass.invokemethod(expandometaclass.java:1110) | error @ groovy.lang.metaclassimpl.invokemethod(metaclassimpl.java:1016) | error @ groovy.lang.closure.call(closure.java:423) | error @ groovy.lang.closure.call(closure.java:439) | error @ groovyx.net.http.httpbuilder.parseresponse(httpbuilder.java:561) | error @ groovyx.net.http.httpbuilder.dorequest(httpbuilder.java:494) | error ... 113 more done
i have googled , found out it's problem related groovy , problem not occurring before version 2.3.0, this post saying.
i tried answers , hints none of them helped. can make json parseable?
you trying fetch data incorrect url, see here. should be:
https://www.evisitor.hr/evisitorrhetos_api/rest/htz/country/?psize=20&page=1
there's no test_api
part. no page found results in 404
, this answer explains exception get.
the script below (with changed url) works - gives 401
since don't send authentication data:
@grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1') import groovyx.net.http.httpbuilder import groovyx.net.http.contenttype import static groovyx.net.http.method.get def http = new httpbuilder("https://www.evisitor.hr/evisitorrhetos_api/rest/") http.request(get, contenttype.json) { uri.path = "htz/country/?psize=20&page=1" headers.accept = 'application/json' headers.'user-agent' = 'mozilla/5.0 ubuntu/8.10 firefox/3.0.4' response.success = { respnse, reader -> string text = resp.entity.content.text println("text is: " + text) string contenttype = resp.headers."content-type" println("content type is: " + contenttype) if (contenttype?.startswith("application/json")) { def json = jsonslurper().parsetext(text) println(json) println("1") } else { println("2") } } response.failure = { resp -> println resp.status } }
also, api invalid, requested response format - accept
header - application/json
, result returned in html
- that's why http-builder
fails - expects different response format. eliminate exception change response.failure
closure to:
{ resp -> println resp.status }
eliminate reader
param - prevent http-builder parsing response automatically.
Comments
Post a Comment