POST with HTTPBuilder -> NullPointerException?

后端 未结 3 1940
终归单人心
终归单人心 2021-01-04 22:18

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

3条回答
  •  自闭症患者
    2021-01-04 22:58

    If you need to execute a POST with contentType JSON and pass a complex json data, try to convert your body manually:

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

提交回复
热议问题