简体   繁体   English

如何使用Android批注和RestTemplate在POST请求的正文中发送x-www-form-urlencoded

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

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. 我尝试传递一个字符串,但我只得到: "login=myName&password=myPassword&key=othereKey"但是由于配额符号,这是错误的。

If I understand correctly, you want to post login and password parameters from a form to your method. 如果我理解正确,则希望将loginpassword参数从表单发布到您的方法中。

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. 创建一个登录表单,该表单具有输入文本字段,其中包含loginpassword
  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. 确保form具有POST方法,您实际上并不想在URL中使用用户凭据作为获取参数,但是如果用例需要这样做,则可以。
  3. In your Interface , instead of using GsonHttpMessageConverter you should be using FormHttpMessageConverter . 在你的Interface ,而不是使用GsonHttpMessageConverter你应该使用FormHttpMessageConverter This converter accepts and returns content with application/x-www-form-urlencoded which is the correct content-type for form submissions. 此转换器接受并返回具有application/x-www-form-urlencodedcontent-type ,该content-type是表单提交的正确content-type
  4. Your Param class should have fields which have the same name as the input text fields. 您的Param类应具有与输入文本字段同名的字段。 In your case, login and password . 在您的情况下,请输入loginpassword After you do this, request parameters posted in the form will be available in the param instance. 完成此操作后,在表单实例中发布的请求参数将可用在param实例中。

Hope this helps. 希望这可以帮助。

  1. Be sure to include FormHttpMessageConverter.class in your converters list. 确保在转换器列表中包含FormHttpMessageConverter.class。
  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. 不要使用Param类型来发送数据,而应使用MultiValueMap实现(例如LinkedMultiValueMap)或使您的Param类扩展LinkedMultiValueMap。

Example extending LinkedMultiValueMap: 扩展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);
    }
}

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. 也就是说,如果您传入MultiValueMap,由于某种原因,它将被添加到标题中,因为Android注释会创建HttpEntity。 If you extend MultiValueMap as Ricardo suggested it will work. 如果按照Ricardo的建议扩展MultiValueMap,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 x-www-form-urlencoded 正文发送 post 请求 - How to send post request with x-www-form-urlencoded body 如何以 application/x-www-form-urlencoded 在 restTemplate 中发送正文 - How to send body in restTemplate as application/x-www-form-urlencoded 如何使用 webclient 发布正文 x-www-form-urlencoded? - how to post body x-www-form-urlencoded using webclient? 如何获取和解析来自 x-www-form-urlencoded POST、RestTemplate (Java) 的 JSON 响应? - How to get and parse JSON response from x-www-form-urlencoded POST, RestTemplate (Java)? FeignClient 使用 application/x-www-form-urlencoded 正文创建 POST - FeignClient create POST with application/x-www-form-urlencoded body Spring - 使用 x-www-form-urlencoded 错误编码 POST 请求 - Spring - wrong encoding POST request with x-www-form-urlencoded 使用x-www-form-urlencoded的Jersey客户端发布请求失败 - Jersey client Post Request with x-www-form-urlencoded Fails 在正文中使用 application/x-www-form-urlencoded 的 400 错误白色 Java Post - 400 error white Java Post using application/x-www-form-urlencoded in body 如何在 HTTPURLConnection 中以 application/x-www-form-urlencoded 格式向 POST 调用添加正文 - How to add a body to POST call in HTTPURLConnection in application/x-www-form-urlencoded format POST x-www-form-urlencoded - POST x-www-form-urlencoded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM