Skip to content
Advertisement

Groovy HTTP ResponseParseException not found

I am trying to make simple GET request using groovy HTTP request. I am expecting JSON response in this form:

{Records:[]}

So far I have done this in my code:

def getCountries(def cookies) {

    if (!cookies) {
        println("cookies list is 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 I am always getting this response:

3
| Error groovyx.net.http.ResponseParseException: Not Found
| Error     at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:500)
| Error     at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:441)
| Error     at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:390)
| Error     at groovyx.net.http.HTTPBuilder$request$0.call(Unknown Source)
| Error     at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
| Error     at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
| Error     at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
| Error     at com.manas.evisitor.EvisitorService$$EPpVE4fg.$tt__getCountries(EvisitorService.groovy:175)
| Error     at com.manas.evisitor.EvisitorService$$DPpVE4fg.$tt__getCountries(Unknown Source)
| Error     at com.manas.evisitor.EvisitorService.$tt__getCountries(EvisitorService.groovy)
| Error     at com.manas.evisitor.EvisitorService$_getCountries_closure3$$EPpVE4fg.doCall(EvisitorService.groovy)
| Error     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| Error     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
| Error     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
| Error     at java.lang.reflect.Method.invoke(Method.java:606)
| Error     at org.springsource.loaded.ri.ReloadedTypeInvoker$2.invoke(ReloadedTypeInvoker.java:122)
| Error     at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1299)
| Error     at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
| Error     at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
| Error     at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1207)
| Error     at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1110)
| Error     at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
| Error     at groovy.lang.Closure.call(Closure.java:423)
| Error     at com.manas.evisitor.EvisitorService$_getCountries_closure3.call(EvisitorService.groovy)
| Error     at groovy.lang.Closure.call(Closure.java:439)
| Error     at com.manas.evisitor.EvisitorService$_getCountries_closure3.call(EvisitorService.groovy)
| Error     at org.codehaus.groovy.grails.orm.support.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:88)
| Error     at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
| Error     at org.codehaus.groovy.grails.orm.support.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:85)
| Error     at com.manas.evisitor.EvisitorService$$EPpVE4fg.getCountries(EvisitorService.groovy)
| Error     at com.manas.evisitor.EvisitorService$$DPpVE4fg.getCountries(Unknown Source)
| Error     at com.manas.evisitor.EvisitorService.getCountries(EvisitorService.groovy)
| Error     at com.manas.evisitor.EvisitorService$getCountries$0.call(Unknown Source)
| Error     at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
| Error     at com.manas.evisitor.EvisitorService$getCountries$0.call(Unknown Source)
| Error     at com.manas.frontdesk.EvisitorController.testevisitor(EvisitorController.groovy:42)
| Error     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| Error     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
| Error     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
| Error     at java.lang.reflect.Method.invoke(Method.java:606)
| Error     at org.springsource.loaded.ri.OriginalClassInvoker.invoke(OriginalClassInvoker.java:47)
| Error     at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1299)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.MixedGrailsControllerHelper.invoke(MixedGrailsControllerHelper.java:154)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleAction(AbstractGrailsControllerHelper.java:375)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.executeAction(AbstractGrailsControllerHelper.java:252)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:205)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:126)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController.handleRequest(SimpleGrailsController.java:72)
| Error     at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:50)
| Error     at org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet.doDispatch(GrailsDispatcherServlet.java:347)
| Error     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
| Error     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
| Error     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
| Error     at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
| Error     at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
| Error     at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
| Error     at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
| Error     at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
| Error     at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
| Error     at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
| Error     at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
| Error     at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
| Error     at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
| Error     at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
| Error     at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
| Error     at org.codehaus.groovy.grails.web.mapping.UrlMappingUtils.forwardRequestForUrlMappingInfo(UrlMappingUtils.java:178)
| Error     at org.codehaus.groovy.grails.web.mapping.UrlMappingUtils.forwardRequestForUrlMappingInfo(UrlMappingUtils.java:144)
| Error     at org.codehaus.groovy.grails.web.mapping.UrlMappingUtils.forwardRequestForUrlMappingInfo(UrlMappingUtils.java:135)
| Error     at org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter.doFilterInternal(UrlMappingsFilter.java:216)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
| Error     at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:69)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
| Error     at org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
| Error     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
| Error     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
| Error     at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
| Error     at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
| Error     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
| Error     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
| Error     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
| Error     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
| Error     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
| Error     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
| Error     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
| Error     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
| Error     at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
| Error     at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
| Error     at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
| Error     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
| Error     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
| Error     at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
| Error     at java.lang.Thread.run(Thread.java:745)
| Error Caused by: groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object
The current character read is '<' with an int value of 60
Unable to determine the current character, it is not a 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     at groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:216)
| Error     at groovy.json.internal.JsonParserCharArray.decodeValue(JsonParserCharArray.java:166)
| Error     at groovy.json.internal.JsonParserCharArray.decodeFromChars(JsonParserCharArray.java:45)
| Error     at groovy.json.internal.JsonParserCharArray.parse(JsonParserCharArray.java:409)
| Error     at groovy.json.internal.BaseJsonParser.parse(BaseJsonParser.java:121)
| Error     at groovy.json.JsonSlurper.parse(JsonSlurper.java:224)
| Error     at groovyx.net.http.ParserRegistry.parseJSON(ParserRegistry.java:280)
| Error     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
| Error     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
| Error     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
| Error     at java.lang.reflect.Method.invoke(Method.java:606)
| Error     at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1276)
| Error     at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
| Error     at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
| Error     at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1207)
| Error     at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1110)
| Error     at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1074)
| Error     at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1110)
| Error     at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
| Error     at groovy.lang.Closure.call(Closure.java:423)
| Error     at groovy.lang.Closure.call(Closure.java:439)
| Error     at groovyx.net.http.HTTPBuilder.parseResponse(HTTPBuilder.java:561)
| Error     at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:494)
| Error     ... 113 more
done

I have googled and found out that it’s problem related to groovy and this problem was not occurring before version 2.3.0, like this post is saying.

I tried all answers and hints but none of them helped. What can I do to make my JSON parseable?

Advertisement

Answer

You are trying to fetch data from 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 and this answer explains the exception you get.

The script below (with changed URL) works – it gives 401 since I 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, the API is invalid, the requested response format – Accept header – is application/json and the result is returned in HTML – that’s why http-builder fails – it expects different response format. The eliminate the exception change response.failure closure to:

{ resp ->
   println resp.status
}

eliminate the reader param – it will prevent http-builder from parsing the response automatically.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement