Quantcast

Sending Success response after JSON request

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Sending Success response after JSON request

Eric Ettes
Hi all,

I'm currently trying out some messaging with Grails using JSON, and it seems to work pretty good. There's one thing that I don't seem to get right, and that's sending a success response after the JSON request has been processed.

The code consists of two independent applications with a sentJSON and a parseJSON function:

Sender:

    def sendJSON() {

        def url = 'http://localhost:8090/jsonreceiver/parsejson'
        def obj = new TestObject(params)
        def objJson = obj as JSON

        def http = new HTTPBuilder(url)
        http.request(POST, groovyx.net.http.ContentType.JSON) { req ->
            body = objJson.toString()
            response.success = { resp, json ->
                println "Request succesful with status ${resp.status}"
            }
            response.failure = { resp ->
                println "Request failed with status ${resp.status}"
            }
        }
        redirect action: 'edit'
    }

Receiver:

    def parsejson() {
        def incoming = new TestObject(request.JSON)
        response.sendError(200)
    }

The TestObject is converted, sent and rebuilt fine, but what do I need to do to inform the sending application that all went well? I tried to return the response object, but that gave me a 404. Using response.sendError(500) gave me an Http 500 (obviously) so the applications do 'see' each other :-)

Seeing as there's no response.sendSuccess, I'm probably missing something trivial?

Kind regards,

-- 
Eric Ettes
Sent with Sparrow

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Sending Success response after JSON request

Ian Roberts
On 18/04/2012 11:03, Eric Ettes wrote:
> The TestObject is converted, sent and rebuilt fine, but what do I need
> to do to inform the sending application that all went well? I tried to
> return the response object, but that gave me a 404. Using
> response.sendError(500) gave me an Http 500 (obviously) so the
> applications do 'see' each other :-)
>
> Seeing as there's no response.sendSuccess, I'm probably missing
> something trivial?

If you want just a status code with no response body then you should use
a 204 status code

render status:HTTPServletResponse.SC_NO_CONTENT

If you want to send a 200 code then that needs a body of some kind, so
something like

render [result:"OK"] as JSON

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Sending Success response after JSON request

Eric Ettes
Thanks Ian,

That did the trick, I'm receiving the status message in my success handler, together with a HTTP 200 status!

Kind regards,

-- 
Eric Ettes
Sent with Sparrow

On Wednesday, April 18, 2012 at 12:18 PM, Ian Roberts wrote:

On 18/04/2012 11:03, Eric Ettes wrote:
The TestObject is converted, sent and rebuilt fine, but what do I need
to do to inform the sending application that all went well? I tried to
return the response object, but that gave me a 404. Using
response.sendError(500) gave me an Http 500 (obviously) so the
applications do 'see' each other :-)

Seeing as there's no response.sendSuccess, I'm probably missing
something trivial?

If you want just a status code with no response body then you should use
a 204 status code

render status:HTTPServletResponse.SC_NO_CONTENT

If you want to send a 200 code then that needs a body of some kind, so
something like

render [result:"OK"] as JSON

Ian

--
Ian Roberts | Department of Computer Science
[hidden email] | University of Sheffield, UK

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


Loading...