How to correctly access the request contentType in a filter?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to correctly access the request contentType in a filter?

marcopas
Hi there, i am trying to access the contentType for a HTTP Post
request that is send via a HTTPBuilder but the request.contentType
property which i receive always seems null. When i try to use another
program to make a http post then the contentType is correct.

The filter accesses the contentType via:
request.contentType

Is this the correct way?

I am sending the http post via httpbuilder using:

            def http = new HTTPBuilder(baseUrl)
            def result = null

            // perform a ${method} request, expecting TEXT response
            http.request(Method.POST) {
                requestContentType = groovyx.net.http.ContentType.JSON
                uri.path = path
                uri.query = query

                // add possible headers
                requestHeaders.each { key, value ->
                    headers."${key}" = "${value}"
                }

                // response handler for a success response code
                response.success = { resp, reader ->
                    result = reader.getText()
                }
            }

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: How to correctly access the request contentType in a filter?

Antti O.
Hi,
This is a reply to an old message from Feb 25, 2013 but maybe the next person to search for this problem benefits from this :)

You might be sending an empty header line in the request and that's why the parser side handles the request incorrectly. I had the same problem with Grails Rest-Client-Builder plugin: Grails application's filter got null from request.contentType. The invalid HTTP request was:

    PUT /myurl HTTP/1.1
    Accept: text/plain, */*
    X-Username
    Content-Type: application/json
    User-Agent: Java/1.7.0_55
    Host: localhost:8080
    Connection: close
    Content-Length: 0

When I removed the "X-Username" header (or gave it a value) the application was able to read the "Content-Type" header value in the filter.


--
Antti


marcopas wrote
Hi there, i am trying to access the contentType for a HTTP Post
request that is send via a HTTPBuilder but the request.contentType
property which i receive always seems null. When i try to use another
program to make a http post then the contentType is correct.

The filter accesses the contentType via:
request.contentType

Is this the correct way?

I am sending the http post via httpbuilder using:

            def http = new HTTPBuilder(baseUrl)
            def result = null

            // perform a ${method} request, expecting TEXT response
            http.request(Method.POST) {
                requestContentType = groovyx.net.http.ContentType.JSON
                uri.path = path
                uri.query = query

                // add possible headers
                requestHeaders.each { key, value ->
                    headers."${key}" = "${value}"
                }

                // response handler for a success response code
                response.success = { resp, reader ->
                    result = reader.getText()
                }
            }

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

    http://xircles.codehaus.org/manage_email