Chuck L
Chuck L

Reputation: 1124

How to access form parameters using RestEasy REST.Request?

I have my form below:

<form id="form1" name="form1">
    <fieldset>
        <ol>
            <li>
                <label>Project:</label>
                <select id="project" name="project" required></select>
            </li>
        </ol>
        <input type="button" onclick="request();">
    </fieldset>
</form>

I created a request() function on the onclick event of the input button. I changed the input type to button because I didn't want the page to reload.

The request function uses the RestEasy REST.Request class to create a custom rest request. I used setEntity on the REST.Request object but I don't know how to access that information on the server side.

Below is the request function:

request: function() {
    var myRequest = new REST.Request();
    var form1 = document.forms["form1"];

    var projectTxt = form1.elements["project"].value;

    myRequest.setURI(REST.apiURL + "/request/item");
    myRequest.setMethod("GET");
    myRequest.setEntity({project:projectTxt});

    myRequest.execute(function(status, request, entity) {
        if (status === 200 && request.readyState === 4) {
            // entity is always null
            sessionStorage.setItem("project", entity);
            console.log("entity=" + entity);
        }
    });
},

In the above code, entity in the function passed to myRequest.execute() is always null.

Here is the Java code:

@Path("item")
@GET
public String itemFormatRequest(@FormParam("project") String project)
{
    // project is always null
    return "blarg!!! project is " + project;
}

In the above code, project is null. I've tried using @QueryParam and that doesn't work either. Am I just using this incorrectly or is there something else I'm missing? I've done much trial and error by changing @GET to @POST in both the javascript and java codes. Have also tried adding @Consumes("application/x-www-form-urlencoded") in the java code and that didn't work.

The only thing I did get to work is adding query parameters to the REST.request object like this:

myRequest.addQueryParameter("project", projectTxt);

And then I'm able to retrieve this using (@QueryParam("project") String project).

Thanks!

Upvotes: 0

Views: 1004

Answers (1)

lefloh
lefloh

Reputation: 10981

You are using GET as request method. A GET is intended to retrieve data and so you don't pass an entity. If you just want to read data of a selected project you can use a GET in conjunction with a @QueryParam. But then you don't pass an entity. Just add the queryParameter:

myRequest.addQueryParameter('project', projectTxt);

If you want to pass data to the server you should change the method of the request to POST (in the client and the server code). Also your entity is JSON but your server code expects a @FormParam. The REST.Request object has a addForm and add addFormParameter method. So one of the following should also work (untested):

myRequest.addForm('form1', form1);

or

myRequest.addFormParameter('project', projectTxt);

Upvotes: 0

Related Questions