proera
proera

Reputation: 133

UTF-8 Bad request

I have a String with the character "Á" (spanish language) and I have some problem with the request to the API.

In this way I get the 400 ERROR BAD REQUEST

String address = "Ángel Marsa I Becá è";
String encoded = new String(address.getBytes("UTF-8"));
System.out.println(encoded);//�ngel Marsa I Becá è

Using antoher manner to encode, with ISO-8859-1 and then UTF-8 I don't get the BAD REQUEST 400 ERROR.

byte ptext[] = address.getBytes("ISO-8859-1"); 
String value = new String(ptext, "UTF-8");
System.out.println(value);//?ngel Marsa I Bec? ?

Is this way encoded UTF8 correctly?

Which is the best way to encode to UTF-8, specially strings with accents, or with "ñ".

Thanks

Upvotes: 1

Views: 1613

Answers (2)

proera
proera

Reputation: 133

I was missing the parameter charset=utf-8 to the Content-Type.

Upvotes: 0

tentacle
tentacle

Reputation: 553

You can use java.net.URLEncoder to pass strings that are not otherwise acceptable in the URL.

String s = "Ángel Marsa I Becá è";
String e = URLEncoder.encode(s, "UTF-8");
System.out.println(e);

This will look like %C3%81ngel+Marsa+I+Bec%C3%A1+%C3%A8.

Once you get it over the network, you always can restore original text with UrlDecoder.decode.

Upvotes: 1

Related Questions