简体   繁体   English

我正在发送此 http 请求,但它传递的是 % 而不是 @

[英]I'm sending this http request but its passing % instead of @

This is the line where I'm sending a patch request这是我发送补丁请求的行

**request.header("Content-Type", "application/x-www-form-urlencoded");
request.body(requestParams.toString());
Response response = request.patch("/home/taha%40gmail.com");**

its failing because its passing %40 instead of @ between email. when I checked the logs (taha%40gmail.com)它失败是因为它在 email 之间传递了 %40 而不是 @。当我检查日志时 (taha%40gmail.com)

How can I handle this in java?在 java 中我该如何处理?

[1] [1]
To properly encode the email address in the URL, you can use the URLEncoder class from the java.net package. Here is an example of how you can do this:要在 URL 中正确编码 email 地址,您可以使用java.net package 中的URLEncoder class。以下是如何执行此操作的示例:

import java.net.URLEncoder;



String email = "taha@gmail.com";
String encodedEmail = URLEncoder.encode(email, StandardCharsets.UTF_8);
Response response = request.patch("/home/" + encodedEmail);

This will encode the email address properly, so that the @ symbol is not interpreted as a URL parameter separator.这将正确编码 email 地址,这样 @ 符号就不会被解释为 URL 参数分隔符。

Note that the URLEncoder.encode() method takes two arguments: the string to encode and the character encoding to use.请注意,URLEncoder.encode() 方法采用两个 arguments:要编码的字符串和要使用的字符编码。 In this example, we are using the UTF-8 encoding, which is a widely used character encoding that can handle most characters.在此示例中,我们使用 UTF-8 编码,这是一种广泛使用的字符编码,可以处理大多数字符。

[2] [2]

If the above is not working:如果以上不起作用:

It looks like the email address is being double-encoded, which is causing the % symbol to appear in the email address.看起来 email 地址被双重编码,这导致%符号出现在 email 地址中。

To fix this, you can decode the email address before sending the request.要解决此问题,您可以在发送请求之前解码 email 地址。 Here is an example of how you can do this:以下是如何执行此操作的示例:

import java.net.URLDecoder;

String email = "taha@gmail.com";
String encodedEmail = URLEncoder.encode(email, StandardCharsets.UTF_8);
String decodedEmail = URLDecoder.decode(encodedEmail, StandardCharsets.UTF_8);
Response response = request.patch("/home/" + decodedEmail);

This will first encode the email address using the URLEncoder class, and then decode it using the URLDecoder class. This should remove the double-encoding and allow the email address to be passed correctly in the URL.这将首先使用 URLEncoder class 对 email 地址进行编码,然后使用 URLDecoder class 对其进行解码。这应该删除双重编码并允许 email 地址在 URL 中正确传递。

Because Rest-Assured does URLencode out of the box, then you need to explicitly stop it.因为 Rest-Assured 开箱即用 URLencode,所以您需要明确停止它。

request.urlEncodingEnabled(false).patch("/home/taha%40gmail.com");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM