I'm posting a text file that contains a list on multiple lines to my service via curl. When I read the body of the request in my Spring MVC controller there are no new line characters and all text is on one line.
Does curl remove new line characters? Is there a way to maintain the new line characters as I need them to parse the text. Here is a snippet code that I am using to read the post body:
StringBuilder builder = new StringBuilder(); String line = null; try { BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) {// this is just one line of text! builder.append(line); } } catch (Exception e) { //handle exception }
You need to advance to the next line on the page AND return to the beginning of the line, hence CRLF , two characters.
Adding Newline Characters in a String In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.
Yes, curl removes linebreaks from data posted with the -d option. Use --data-binary instead. See SO question How to send line break with curl? for more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With