Grails-向API发送“多部分/表单数据”请求

时间:2018-07-23 08:29:23

标签: api grails kanban

我正在尝试使用Kanbanize API向票证添加附件,但是它们的文档确实很糟糕,而且我不知道如何将规范转换为有效的Grails代码。该文档可以在here中找到。这是我尝试过的:

def addAttachment(ticket, attName, attType, attBytes) {

    // println attName // outputs " capital.pdf "
    // println attType // outputs " application/pdf "
    // println attBytes // outputs " [37, 80, 68, ...] ", assume it's a valid byte array

    try {
        def http = new HTTPBuilder("https://examplesubdomain.kanbanize.com/index.php/api/kanbanize/add_attachment")
        http.request( Method.POST, groovyx.net.http.ContentType.JSON ) { req ->
            headers = [
                'apikey':'secretApiKey',
                'content-type': 'multipart/form-data; boundary=AnyStringNotFoundInTheBody'
            ]
            body = [
                params: [
                    boardid:123,
                    taskid:123,
                ],
                'Content-Disposition': ''+attBytes+'; name="'+attName+'"; filename="'+attName+'"',
                'Content-Type': attType,
            ]
            response.success = { resp, json ->
                ret = json
            }
        }
        println ret // This statement is never reached
    } catch(all) {
        all.printStackTrace()
    }
}

在上面的代码中,我打印attNameattTypeattBytes,以显示它们的外观,我从一个上传表单中接收了这些值。如果以上内容不足以回答问题,请告诉我,我也将上传上传表单的实现。 另外,我在上面的代码中检查了以下值:

  • examplesubdomain在HTTPBuilder的URL中。
  • 标题中的
  • secretApiKey
  • boardid。为简单起见。
  • taskid。为简单起见。

上面的代码产生的错误:

Error |
groovyx.net.http.HttpResponseException: Bad Request
Error |
    at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:652)
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:1265)
Error |
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
Error |
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
Error |
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1082)
Error |
    at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1106)
Error |
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:949)
Error |
    at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1106)
Error |
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:906)
Error |
    at groovy.lang.Closure.call(Closure.java:412)
Error |
    at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:508)
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.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 unipoint.club.service.KanbanizeService$$EQyblqIi.addAttachments(KanbanizeService.groovy:84)
Error |
    at unipoint.club.service.KanbanizeService$$DQyblqIi.__execute(Unknown Source)
Error |
    at unipoint.club.service.KanbanizeService.__execute(KanbanizeService.groovy)
Error |
    at unipoint.club.service.KanbanizeService$$FastClassByCGLIB$$e7c466db.invoke(<generated>)
Error |
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
Error |
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
Error |
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
Error |
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
Error |
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
Error |
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
Error |
    at unipoint.club.service.KanbanizeService$$EnhancerByCGLIB$$89b01036$$E0.addAttachments(<generated>)
Error |
    at unipoint.club.service.KanbanizeService$$EnhancerByCGLIB$$89b01036$$D0.__execute(Unknown Source)
Error |
    at unipoint.club.service.KanbanizeService$$EnhancerByCGLIB$$89b01036.__execute(<generated>)
Error |
    at unipoint.club.service.KanbanizeService$addAttachments.call(Unknown Source)
Error |
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
Error |
    at unipoint.club.service.KanbanizeService$addAttachments.call(Unknown Source)
Error |
    at unipoint.club.control.admin.AdminKanbanizeController$_logTicket_closure1$$EQybhHWS.doCall(AdminKanbanizeController.groovy:69)
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:1288)
Error |
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
Error |
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
Error |
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1082)
Error |
    at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1106)
Error |
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:906)
Error |
    at groovy.lang.Closure.call(Closure.java:412)
Error |
    at unipoint.club.control.admin.AdminKanbanizeController$_logTicket_closure1.call(AdminKanbanizeController.groovy)
Error |
    at groovy.lang.Closure.call(Closure.java:425)
Error |
    at unipoint.club.control.admin.AdminKanbanizeController$_logTicket_closure1.call(AdminKanbanizeController.groovy)
Error |
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1326)
Error |
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1298)
Error |
    at org.codehaus.groovy.runtime.dgm$148.invoke(Unknown Source)
Error |
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:271)
Error |
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53)
Error |
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
Error |
    at unipoint.club.control.admin.AdminKanbanizeController$$EQybhHWS.logTicket(AdminKanbanizeController.groovy:66)
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:1288)
Error |
    at org.codehaus.groovy.grails.web.servlet.mvc.MixedGrailsControllerHelper.invoke(MixedGrailsControllerHelper.java:69)
Error |
    at grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.invoke(ProxyAwareMixedGrailsControllerHelper.java)
Error |
    at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleAction(AbstractGrailsControllerHelper.java:348)
Error |
    at grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.handleAction(ProxyAwareMixedGrailsControllerHelper.java)
Error |
    at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.executeAction(AbstractGrailsControllerHelper.java:231)
Error |
    at grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.executeAction(ProxyAwareMixedGrailsControllerHelper.java)
Error |
    at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:197)
Error |
    at grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.handleURI(ProxyAwareMixedGrailsControllerHelper.java)
Error |
    at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:118)
Error |
    at grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.handleURI(ProxyAwareMixedGrailsControllerHelper.java)
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:48)
Error |
    at org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet.doDispatch(GrailsDispatcherServlet.java:328)
Error |
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
Error |
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
Error |
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
Error |
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
Error |
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
Error |
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:200)
Error |
    at grails.plugin.cache.web.filter.ehcache.EhcachePageFragmentCachingFilter.doFilter(EhcachePageFragmentCachingFilter.java)
Error |
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
Error |
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
Error |
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
Error |
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
Error |
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
Error |
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
Error |
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
Error |
    at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:318)
Error |
    at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:283)
Error |
    at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:274)
Error |
    at org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter.doFilterInternal(UrlMappingsFilter.java:203)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
Error |
    at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.obtainContent(GrailsPageFilter.java:206)
Error |
    at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.doFilter(GrailsPageFilter.java:152)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
Error |
    at javax.servlet.FilterChain$doFilter.call(Unknown Source)
Error |
    at org.grails.plugin.resource.DevModeSanityFilter.doFilter(DevModeSanityFilter.groovy:45)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:369)
Error |
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
Error |
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
Error |
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
Error |
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
Error |
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:119)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
Error |
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
Error |
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
Error |
    at org.codehaus.groovy.grails.plugins.springsecurity.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:40)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
Error |
    at org.codehaus.groovy.grails.plugins.springsecurity.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:79)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
Error |
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
Error |
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
Error |
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:168)
Error |
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
Error |
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
Error |
    at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:69)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
Error |
    at org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:66)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
Error |
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
Error |
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
Error |
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
Error |
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
Error |
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
Error |
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
Error |
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
Error |
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
Error |
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
Error |
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
Error |
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
Error |
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
Error |
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
Error |
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
Error |
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
Error |
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:622)
Error |
    at java.lang.Thread.run(Thread.java:748)

我看到不允许使用标签api,但是没有kanbanize-apiapi-request标签,因此看起来确实是最合适的。

如果我忘了任何事情并且需要提供更多详细信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

这是Kanbanize研发团队的Milen。我不熟悉Grails,所以我将无法提供有效的代码,但是我可以指出您所给代码中的一些错误。

  1. 身体各部位之间缺少边界线。每个部分应以--AnyStringNotFoundInTheBody开头,正文应以--AnyStringNotFoundInTheBody--结尾 (您可以根据自己的喜好替换AnyStringNotFoundInTheBody,前提是它是主体中唯一的字符串)
  2. params部分未进行JSON编码
  3. Content-Disposition的值应为form-data,并且图像的实际二进制数据应放在末尾。

这将有助于打印您发送的请求的文本表示形式,并将其与我们在文档中提供的示例进行比较。 同样,当API拒绝请求时,通常会出现指出具体原因的错误。因此,打印响应也将有所帮助。

让我们知道您是否需要进一步的帮助和/或直接通过kanbanize.com与我们联系