Grails 使用HTTPBuilder发布->;NullPointerException?

Grails 使用HTTPBuilder发布->;NullPointerException?,grails,post,groovy,httpbuilder,Grails,Post,Groovy,Httpbuilder,我正在尝试发出一个简单的HTTPPOST请求,但我不知道下面的失败原因。我试着遵循这些例子,但我看不出我错在哪里 例外情况 java.lang.NullPointerException at groovyx.net.http.HTTPBuilder$RequestConfigDelegate.setBody(HTTPBuilder.java:1131) ... 代码 def List<String> search(String query, int maxResul

我正在尝试发出一个简单的HTTPPOST请求,但我不知道下面的失败原因。我试着遵循这些例子,但我看不出我错在哪里

例外情况

java.lang.NullPointerException
    at groovyx.net.http.HTTPBuilder$RequestConfigDelegate.setBody(HTTPBuilder.java:1131)
    ...
代码

def List<String> search(String query, int maxResults)
{
    def http = new HTTPBuilder("mywebsite")

    http.request(POST) {
        uri.path = '/search/'
        body = [string1: "", query: "test"]
        requestContentType = URLENC

        headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4'

        response.success = { resp, InputStreamReader reader ->
            assert resp.statusLine.statusCode == 200

            String data = reader.readLines().join()

            println data
        }
    }
    []
}
def列表搜索(字符串查询,int-maxResults)
{
def http=新的HTTPBuilder(“我的网站”)
http.request(POST){
uri.path='/search/'
body=[string1:,查询:“test”]
requestContentType=URLENC
标题。'User-Agent'=“Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4”
response.success={resp,InputStreamReader->
assert resp.statusLine.statusCode==200
字符串数据=reader.readLines().join()
println数据
}
}
[]
}
这项工作:

    http.request(POST) {
        uri.path = '/search/'

        send URLENC, [string1: "", string2: "heroes"]

我发现在分配正文之前有必要设置内容类型。使用groovy 1.7.2,这对我来说是可行的:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0' )
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

def List<String> search(String query, int maxResults)
{
    def http = new HTTPBuilder("mywebsite")

    http.request(POST) {
        uri.path = '/search/'
        requestContentType = URLENC
        headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4'
        body = [string1: "", query: "test"]

        response.success = { resp, InputStreamReader reader ->
            assert resp.statusLine.statusCode == 200

            String data = reader.readLines().join()

            println data
        }
    }
    []
}
@Grab(group='org.codehaus.groovy.modules.httpbuilder',module='http-builder',version='0.5.0')
导入groovyx.net.http*
导入静态groovyx.net.http.ContentType*
导入静态groovyx.net.http.Method*
定义列表搜索(字符串查询,int-maxResults)
{
def http=新的HTTPBuilder(“我的网站”)
http.request(POST){
uri.path='/search/'
requestContentType=URLENC
标题。'User-Agent'=“Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4”
body=[string1:,查询:“test”]
response.success={resp,InputStreamReader->
assert resp.statusLine.statusCode==200
字符串数据=reader.readLines().join()
println数据
}
}
[]
}

如果需要使用contentType JSON执行POST并传递复杂的JSON数据,请尝试手动转换正文:

def attributes = [a:[b:[c:[]]], d:[]] //Complex structure
def http = new HTTPBuilder("your-url")
http.auth.basic('user', 'pass') // Optional
http.request (POST, ContentType.JSON) { req ->
  uri.path = path
  body = (attributes as JSON).toString()
  response.success = { resp, json -> }
  response.failure = { resp, json -> }
}    

这个给我修好了。使用
send-URLENC、[string1:,string2:“heroes”]
也可以工作,但在模拟HTTPBuilder时,单元测试会更加困难。