Home > Net >  I'm sending this http request but its passing % instead of @
I'm sending this http request but its passing % instead of @

Time:12-26

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/[email protected]");**

its failing because its passing @ instead of @ between email. when I checked the logs ([email protected])

How can I handle this in java?

CodePudding user response:

Because Rest-Assured does URLencode out of the box, then you need to explicitly stop it.

request.urlEncodingEnabled(false).patch("/home/[email protected]");

CodePudding user response:

[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:

import java.net.URLEncoder;



String email = "[email protected]";
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.

Note that the URLEncoder.encode() method takes two arguments: the string to encode and the character encoding to use. In this example, we are using the UTF-8 encoding, which is a widely used character encoding that can handle most characters.

[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.

To fix this, you can decode the email address before sending the request. Here is an example of how you can do this:

import java.net.URLDecoder;

String email = "[email protected]";
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.

  • Related