I am posting data with embedded double quotes from a libcurl client to a Tomcat servlet.
The servlet reads the data and sends it back to my client.
What I send looks like this
{
"id": 1,
"name": "Foo",
}
But what I receive from the servlet looks like this
{
\"id\": 1,
\"name\": \"Foo\"
}
i.e. all the double quotes get escaped.
How do I prevent the servlet from escaping the quotes?
My servlet is doing something like this inside the doPost method
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
BufferedReader reader = request.getReader();
StringBuilder content = new StringBuilder();
char[] buffer = new char[4 * 1024];
int len = 0;
while (len >= 0) {
len = reader.read(buffer, 0, buffer.length);
if (len > 0) {
content.append(buffer, 0, len);
}
}
PrintWriter out = response.getWriter();
out.print(content.toString())
}
Edit
This fixes my problem.
URLEncoder.encode(content.toString(),"UTF-8")
Encode the string before you write it.