question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sending special characters

See original GitHub issue

I would like to send a String containing special characters "ä ö ü ß 😀"

Before doing anything complex, I tried

TelegramBot bot = new TelegramBot(System.getenv("TELEGRAM_BOT_TOKEN"));
SendResponse response = bot.execute(new SendMessage(1076643869, "ä ö ü ß 😀"));
System.out.println(response.message().text());

from which I receive a message containing ä ö ü ß 😀 and ä ö ü ß 😀 in the console.

I suspect that there is no fundamental error in my charset, since sending a HTTP POST request manually does not produce the error:

OutputStreamWriter writer = null;
BufferedReader reader = null;
try {
	URL url = new URL("https://api.telegram.org/bot" + System.getenv("TELEGRAM_BOT_TOKEN") + "/sendMessage");
	String payload = "{\"chat_id\": 1076643869, \"text\": \"ä ö ü ß 😀\"}";
	
	HttpURLConnection con = (HttpURLConnection) url.openConnection();
	con.setRequestMethod("POST");
	con.setDoInput(true);
	con.setDoOutput(true);
	con.setUseCaches(false);
	con.setRequestProperty("Content-Type", "application/json");
	con.setRequestProperty("Content-Length", String.valueOf(payload.length()));
	
	writer = new OutputStreamWriter(con.getOutputStream());
	writer.write(payload);
	writer.flush();
	
	reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
	
	StringBuilder builder = new StringBuilder();
	for (String line; (line = reader.readLine()) != null; ) {
		builder.append(line);
	}
	
} catch (IOException e) {
	e.printStackTrace();
} finally {
	try {
		if (writer != null)
			writer.close();
		if(reader != null)
			reader.close();
	}  catch (IOException e) {
		System.err.format("Cant close writer or reader: %s%n", e);
	}
}

sending a message containing ä ö ü ß 😀

Thanks to pengrad’s quick reply to my email, I could figure out that I am using the default windows charset;

System.out.println(System.getProperty("file.encoding")); // Prints windows-1252
System.out.println(Charset.defaultCharset().name()); // Prints windows-1252

A stated above, I don’t think that changing the charset via the VM options would fix the issue, although pengrad stated that it works fine for him (at least using “ä, ö, ü and ß”)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
torbeneimscommented, May 1, 2020

Unfortunately, I did not. To be honest, I have no clue what to do other than manually overwriting, thus changing the library used to execute the request…

0reactions
JVMBuildercommented, Aug 14, 2022

Please add encoding UTF-8

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sending Special Characters - Payment API - Ratepay
There are some special characters that are used by the XML syntax. If used within elements and attributes they needed to be escaped...
Read more >
Using special characters, Unicode, and GSM-T charsets for ...
Using special characters, Unicode, and GSM-T charsets for sending SMS in different languages ... SMS can be sent out using different charsets or ......
Read more >
Special characters supported in email - Acoustic Help Center
Some special characters, such as hearts, spades, diamonds, and copyright symbols, display in emails, but only if these are within the defined ...
Read more >
Sending special characters using HttpURLConnection in Java
I have a web service and I want to invoke that with "application/x-www-form-urlencoded" content type. The request sometimes contains special ...
Read more >
send special characters - the Tcler's Wiki!
Stefan Finzel. Now and then there is the need to handle special characters like CTRL-C e.g. to send special command sequences to devices...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found