Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request params and PUT method

I'm trying to get request params passed by PUT request, at Grails-based app.

I'm using following client code to make request:

$.ajax({
    url: 'api/controllerName/anId',
    type: 'PUT',
    data: $('form').serialize()
})

with following mapping:

"/api/$controller/$id?" {
    action = [ GET: "read", POST: "create", PUT: "update", DELETE: "delete"]
}

But my controller's action receives empty params list, with only id value. I tried to put it content to logs and saw only:

[id:anId, action:[GET:read, POST:create, PUT:update, DELETE:delete], controller:controllerName]

and request.getParameterNames() returns empty list of values.

As I see from FireBug, request contains this params, and have Content-Type as application/x-www-form-urlencoded; charset=UTF-8

If I'm using GET/POST method - everything is working as expected, I can get all passed parameters.

How I can get access to passed parameters?

Update: I've just figured that PUT implies passing data as JSON/XML in body. Btw, this question is still actual, just in case

like image 467
Igor Artamonov Avatar asked Aug 25 '11 19:08

Igor Artamonov


People also ask

Can put HAVE request params?

Is it OK to use query parameters in a PUT request? Absolutely. Query parameters are just another piece of the resource identifier.

What is put method in HTTP?

The HTTP PUT request method is used to create a new resource or overwrite a representation of the target resource that is known by the client. Calling this method once is similar to calling it multiple times successively as it has the same effect. Though it is idempotent, we cannot cache its response.

Can we pass request body PUT method?

You can send a POST/PUT request without a body and instead use query string parameters. But be careful if your parameters contain characters that are not HTTP valid you will have to encode them. Save this answer.

What is put method in API?

A PUT method puts or places a file or resource precisely at a specific URI. In case a file or a resource already exists at that URI, the PUT method replaces that file or resource. If there is no file or resource, PUT creates a new one.


2 Answers

I had the exact same issue today and was able to fix it. I did an ajax request with JQuery to a Grails RESTful WebService (method: "PUT"), but the parameter map was empty.

Just want to share my solution here.

I had to JSON.stringify my data and set the contentType to "application/json" and then use request.JSON (as suggested before) in my Grails controller.

example (JQuery Ajax Request):

$.ajax({
        url: "/entries/" + id,
        contentType: "application/json",
        type: "PUT",
        dataType: "json",
        data: JSON.stringify({'name' : name, 'date': date}),
        success: function(msg) {
            console.log('updated...')
            $.mobile.changePage($('#list'));
        }

afterwards I could use:

request.JSON

in my Controller in order to get the data sent by the ajax request (params was still empty). If I did not use JSON.stringify and set the content type, I had the following Grails error:

Error 2012-04-03 13:50:20,046 [http-bio-8080-exec-3] ERROR errors.GrailsExceptionResolver  - IllegalStateException occurred when processing request: [PUT] /entries/2
getReader() has already been called for this request. Stacktrace follows:
Message: getReader() has already been called for this request

I'm using Grails 2.0.1 and JQuery 1.7.1

like image 95
mburri Avatar answered Sep 17 '22 13:09

mburri


Try request.JSON (or request.XML) instead of params.

like image 43
Дмитрий Баранов Avatar answered Sep 21 '22 13:09

Дмитрий Баранов