How to send x-www-form-urlencoded in a body of POST request using android annotations and resttemplate

Vicek picture Vicek · Oct 30, 2013 · Viewed 9.3k times · Source

my interface looks as follows:

@Rest(rootUrl = "https://myurl.com", converters = { GsonHttpMessageConverter.class })
public interface CommunicatonInterface
{
@Get("/tables/login")
public Login login(Param param);
public RestTemplate getRestTemplate();
}

The question is what i supposed to put as a param to get in body simply:

login=myName&password=myPassword&key=othereKey

without escaping, brackets or quota.

I've try to pass a string and i just get: "login=myName&password=myPassword&key=othereKey" but it is wrong because of quota signs.

Answer

Akshay picture Akshay · Dec 6, 2013

If I understand correctly, you want to post login and password parameters from a form to your method.

For this, you should ensure you have the following steps:

  1. Create a login form which has input text fields with login and password as names.
  2. Make sure the form has a POST method, you don't really want to have user's credentials in the URL as get params, but if you use case needs you to do it, you can.
  3. In your Interface, instead of using GsonHttpMessageConverter you should be using FormHttpMessageConverter. This converter accepts and returns content with application/x-www-form-urlencoded which is the correct content-type for form submissions.
  4. Your Param class should have fields which have the same name as the input text fields. In your case, login and password. After you do this, request parameters posted in the form will be available in the param instance.

Hope this helps.