使用 HTTPBuilder 发布 ->空指针异常? [英] POST with HTTPBuilder -> NullPointerException?

查看:33
本文介绍了使用 HTTPBuilder 发布 ->空指针异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发出一个简单的 HTTP POST 请求,但我不知道为什么以下内容失败.我尝试按照这里的示例进行操作,但我没有看到我哪里出错了.

I'm trying to make a simple HTTP POST request, and I have no idea why the following is failing. I tried following the examples here, and I don't see where I'm going wrong.

例外

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
        }
    }
    []
}

推荐答案

这有效:

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

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

这篇关于使用 HTTPBuilder 发布 ->空指针异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆