Mauricio Hofmam
Mauricio Hofmam

Reputation: 21

Java code with POST request with newline in Json body issuing an error

I have written a Java client for a REST server that works perfectly unless a newline is added to a String in the Json body request. The same request works perfectly with the newline if I use a client such as Insomnia. A Javascript/HTML client also works fine with the same data. In the Json sample below the problem is with the field "text". If I remove the "\n" the code works. Otherwise the server returns error 400.

I have tried several different encodings and arguments in the request. Nothing seems to work.

Json request formated
{
    "name": "Operation 101",
    "idPio": "10007200000000000205",
    "idGlobalPio": "5387fed1-d010-4bde-b45b-7dd5815b9e03",
    "text": "Description\n1 - First line\n2 - Second Line",
}

My java code

//If I just remove the "\n" from the String below the server issues the 200 code
//But the server will accept this string just as it is if sent form Insomnia, handling correctly the newlines.
String jsonBody = "{\"name\": \"Operation 101\",\"idPio\": \"10007200000000000205\",\"idGlobalPio\": \"5387fed1-d010-4bde-b45b-7dd5815b9e03\",\"text\": \"Descrition\n1 - First line\n2 - Second Line\",}";

url = new URL("https://10.120.43.23:8000/api/item/1.0/item/annotation/?t=E34B2A8A-0A64-469A-AE52-45E8A9885D70");

HttpsURLConnection con = (HttpsURLConnection)url.openConnection();

//Add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Java client");
con.setRequestProperty("Accept-Language", "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7,fr;q=0.6");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
con.setRequestProperty("Accept", "application/json, text/plain, */*");
con.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
con.setRequestProperty("Connection", "keep-alive");
con.setDoOutput(true);

//Prepare data and send
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
byte[] arr = jsonBody.getBytes("UTF-8");
wr.write(arr);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println(String.valueOf(responseCode));

Upvotes: 1

Views: 11738

Answers (3)

Manjeet Brar
Manjeet Brar

Reputation: 943

Replace line break \n with \\n and remove comma (,) from last json item

The correct syntax is as below

   String jsonBody = "{\"name\": \"Operation 101\",\"idPio\": \"10007200000000000205\",\"idGlobalPio\": \"5387fed1-d010-4bde-b45b-7dd5815b9e03\",\"text\": \"Descrition\\n1 - First line\\n2 - Second Line\"}";

Upvotes: 2

Sandy Genedy
Sandy Genedy

Reputation: 226

Replace the line break with \\\n instead of \n.

Upvotes: 4

RoToRa
RoToRa

Reputation: 38431

Keep in mind a literal line break is not allowed inside a string in JSON. It needs to be represented as \n. However line breaks in Java strings are also represented as \n. For example:

A Java String like

String example = "{\"test\":\"line\nbreak\"}"

would represent the (JSON) string

{"test":"line
break"}

which is not not allowed. You need the JSON string to be:

{"test":"line\nbreak"}

which is represented in a Java String as:

String example = "{\"test\":\"line\\nbreak\"}"
// Notice the double backslash ---^^

With outer words: Just like you have to escape the quotes (") in Java strings with a backslash, you also need to escape other backslashes (such as the one in \n).

Upvotes: 3

Related Questions