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

3

6

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.

Katti answered 30/10, 2013 at 11:51 Comment(0)
O
1

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.

Oligopoly answered 6/12, 2013 at 1:43 Comment(0)
A
1
  1. Be sure to include FormHttpMessageConverter.class in your converters list.
  2. Instead of using a Param type for sending the data, use a MultiValueMap implementation (such as LinkedMultiValueMap) or make your Param class extend LinkedMultiValueMap.

Example extending LinkedMultiValueMap:

@Rest(converters = {FormHttpMessageConverter.class, MappingJacksonHttpMessageConverter.class})
public interface RestClient extends RestClientRootUrl {
    @Post("/login")
    LoginResponse login(LoginRequest loginRequest);
}


public class LoginRequest extends LinkedMultiValueMap<String, String> {
    public LoginRequest(String username, String password) {
        add("username", username);
        add("password", password);
    }
}
Avra answered 7/1, 2014 at 23:31 Comment(0)
O
0

You can have multiple converters because based on the object passed in, it will choose a converter for you. That said if you pass in a MultiValueMap it will for some reason add it to the headers because Android Annotations creates a HttpEntity. If you extend MultiValueMap as Ricardo suggested it will work.

Orifice answered 21/2, 2014 at 22:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.