2

I read this and several other postings on SO and elsewhere about how to send a Post call via HttpBuilder with JSON as the data content. My problem is that NONE OF THOSE SOLUTIONS are working!

My problem is only slightly different. I have existing JSON data in a file. When I attempt to send this to the REST interface with curl:

curl -X POST -u "username:password" -d @/path/to/myFile.json http://localhost:8080/path/here --header "Content-Type:application/json" 

all works perfectly well. Here is where I am at (some extra code IS in there, read on):

def myFile = new File('/path/to/myFile.json') if (!myFile.exists()) println "ERROR! Do not have JSON file!" def convertedText = myFile.text.replaceAll('\\{', '[') convertedText = convertedText.replaceAll('\\}', ']') def jsonBldr = new JsonBuilder() jsonBldr myFile.text println jsonBldr.toString() def myClient = new groovyx.net.http.HTTPBuilder('http://username:password@localhost:8080/my/path') myClient.setHeaders(Accept: 'application/json') results = myClient.request(POST, JSON) { req -> body = [ jsonBldr.toString() ] requestContentType = JSON response.success = { resp, reader -> println "SUCCESS! ${resp.statusLine}" } response.failure = { resp -> println "FAILURE! ${resp.properties}" } } 

This results in the 'failure' closure with this data:

statusLine:HTTP/1.1 400 Exception evaluating property 'id' for java.util.ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: id for class: java.lang.String 

FWIW, there is no "id" in my JSON anywhere. If I change the "body" line from "[ jsonBldr.toString() ]" to "[ convertedText ]" - which is why that code is up there, I get the same error. If I take out the brackets on the body, I get an error stating that the body is not data for an array (as its a Map).

Can anyone (far groovier than I) tell me what the %%$#@ I am doing wrong???

1 Answer 1

4

You need JsonSlurper instead of JsonBuilder. After which the implementation would look like:

def myFile = new File('/path/to/myFile.json') if (!myFile.exists()) println "ERROR! Do not have JSON file!" def bodyMap = new JsonSlurper().parseText(myFile.text) def myClient = new groovyx.net.http.HTTPBuilder('http://username:password@localhost:8080/my/path') modelClient.setHeaders(Accept: 'application/json') results = myClient.request(POST, JSON) { req -> requestContentType = JSON body = bodyMap response.success = { resp, reader -> println "SUCCESS! ${resp.statusLine}" } response.failure = { resp -> println "FAILURE! ${resp.properties}" } } 

However, I am not clear what is difference between myFile and modelFile in your code.

Sign up to request clarification or add additional context in comments.

7 Comments

THANK YOU!!! Don't know why, but I had to change 'requestContextType' from JSON to 'URLENC', but now it works!
Separately, the doc (groovy.codehaus.org/gapi/index.html?groovy/json/…) for the JsonBuilder.toString method reads: "Serializes the internal data structure built with the builder to a conformant JSON payload string", so while I really like groovy, I still feel like I am on the outside trying to get in! In other words, given this doc, how was I supposed to know and/or find out how to make this work? Sorry - off topic, just needed to rant!
@joeg it was kind of self explanatory. We are not "building" a json but reading one (slurper). You will get into it in time for sure. :)
@JoeG Also the body never excepts a string. have a look at POST using JSON and the API for JsonGroovyBuilder which is used by HttpBuilder. I hope this would clear up the grey area. :)
While I had read/skimmed that page before, I missed that (now obvious) detail. That helps a lot too - thanks!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.