[groovy-user] HTTPBuilder request( POST, JSON) HELP!!!

432 views
Skip to first unread message

Chris Buckley

unread,
Apr 9, 2010, 4:36:03 PM4/9/10
to us...@groovy.codehaus.org
Hi everyone,

Well I'm having a heck of a time posting JSON and receiving JSON could someone help me out here? (please...)

Here's my code...

      def server = [
            server : [
                  name : "olat",
                  imageId : "3005755",
                  flavorId : "4",
                  metadata : [olat : "olat"]
            ]
        ]

      def http = new HTTPBuilder("${tokenAndPath[1]}/servers.json")
      http.request( POST, JSON ) { req ->
        headers.'X-Auth-Token' = tokenAndPath[0]
       
        send URLENC, server

        req.params.setBooleanParameter 'http.protocol.expect-continue', false

        response.success = { resp, json ->
          display.append "Status Success: ${resp.statusLine}\n"
        }

        response.failure = { resp, json  ->
          display.append "Status Error: ${resp.statusLine}\n"
        }

      }

What I'm getting back from the server is ...
Status Error: HTTP/1.1 415 Unsupported Media Type

Tom Nichols

unread,
Apr 9, 2010, 5:06:58 PM4/9/10
to user
So it sounds like the server doesn't like the content-type header that
you're sending. I've seen several variants used to indicate a JSON
content-type:
application/json
application/javascript
text/javascript

The first is what's used by default for the Accept header. You could
try manually using one of the other two variants by setting the header
in your request config closure:
headers.Accept = 'text/javascript'

Also if you haven't already, try turning on header and wire logging so
you can see the entire request and response as described here:
http://groovy.codehaus.org/modules/http-builder/doc/index.html#Logging_and_Debugging

-Tom

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Chris Buckley

unread,
Apr 9, 2010, 5:42:42 PM4/9/10
to us...@groovy.codehaus.org
Thanks Tom!

that's very handy, and you were right I need to send the content-type and accept headers set to the "application/json"  so one more question for you I'm getting the following response from my request

2010-04-09 14:39:19,000 [http-8080-1] DEBUG http.headers  - >> POST /v1.0/439329/servers.json HTTP/1.1
2010-04-09 14:39:19,001 [http-8080-1] DEBUG http.headers  - >> Accept: application/json
2010-04-09 14:39:19,001 [http-8080-1] DEBUG http.headers  - >> X-Auth-Token:....
2010-04-09 14:39:19,001 [http-8080-1] DEBUG http.headers  - >> Content-Type: application/json
2010-04-09 14:39:19,001 [http-8080-1] DEBUG http.headers  - >> Content-Length: 93
2010-04-09 14:39:19,001 [http-8080-1] DEBUG http.headers  - >> Host: servers.api.rackspacecloud.com
2010-04-09 14:39:19,001 [http-8080-1] DEBUG http.headers  - >> Connection: Keep-Alive
2010-04-09 14:39:19,001 [http-8080-1] DEBUG http.headers  - >> User-Agent: Apache-HttpClient/4.0 (java 1.5)
2010-04-09 14:39:19,001 [http-8080-1] DEBUG http.headers  - >> Accept-Encoding: gzip,deflate
2010-04-09 14:39:19,001 [http-8080-1] DEBUG http.wire  - >> "server=%7Bname%3Dolat%2C+imageId%3D3005755%2C+flavorId%3D4%2C+metadata%3D%7Bolat%3Dolat%7D%7D"
2010-04-09 14:39:19,452 [http-8080-1] DEBUG http.wire  - << "HTTP/1.1 400 Bad Request[EOL]"

my question is this I'm using the URLENC and it's url encoding the the body... can I just send the raw text with send ie. send server?

thanks,

Chris

On Fri, Apr 9, 2010 at 2:06 PM, Tom Nichols <tmni...@gmail.com> wrote:
So it sounds like the server doesn't like the content-type header that
you're sending.  I've seen several variants used to indicate a JSON
content-type:
application/json
application/javascript
text/javascript

The first is what's used by default for the Accept header.  You could
try manually using one of the other two variants by setting the header
in your request config closure:
headers.Accept = 'text/javascript'

Also if you haven't already, try turning on header and wire logging so
you can see the entire request and response as described here:
http://groovy.codehaus.org/modules/http-builder/doc/index.html#Logging_and_Debugging

-Tom

Thanks

On Fri, Apr 9, 2010 at 4:36 PM, Chris Buckley <cnbu...@gmail.com> wrote:
> Hi everyone,
>
> Well I'm having a heck of a time posting JSON and receiving JSON could someone help me out here? (please...)
>
> Here's my code...
>
>       def server = [
>             server : [
>                   name : "olat",
>                   imageId : "3005755",
>                   flavorId : "4",
>                   metadata : [olat : "olat"]
>             ]
>         ]
>
>       def http = new HTTPBuilder("${tokenAndPath[1]}/servers.json")
>       http.request( POST, JSON ) { req ->
>         headers.'X-Auth-Token' = tokenAndPath[0]
>
>         send URLENC, server
>
>         req.params.setBooleanParameter 'http.protocol.expect-continue', false
>
>         response.success = { resp, json ->
>           display.append "Status Success: ${resp.statusLine}\n"
>         }
>
>         response.failure = { resp, json  ->
>           display.append "Status Error: ${resp.statusLine}\n"
>         }
>
>       }
>
> What I'm getting back from the server is ...
>
> Status Error: HTTP/1.1 415 Unsupported Media Type
>
>

Tom Nichols

unread,
Apr 19, 2010, 5:37:54 PM4/19/10
to user
Hi Chris, I just realized I never responded to your question.

So if you want to send the POST data as JSON, don't use send( URLENC,
postBodyObject ). You want to do either body = postBodyObject, which
will automatically encode the server as JSON since that's in your
request(POST, JSON) or send( JSON, postBodyObject ). Where
postBodyObject in your case is that 'server' variable.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


--
You received this message because you are subscribed to the Google Groups "Groovy Users" group.
To post to this group, send email to groov...@googlegroups.com.
To unsubscribe from this group, send email to groovy-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/groovy-user?hl=en.

Chris Buckley

unread,
Apr 19, 2010, 5:45:30 PM4/19/10
to us...@groovy.codehaus.org
Thanks Tom.
Reply all
Reply to author
Forward
0 new messages