0

In Java, i only want to read the body information, not the headers, from a curl command.

Currently, i'm using this as the curl command:

curl -H 'Content-Type: text/plain; charset=UTF-8' -F file=@last_commit_log.txt -X POST "http://localhost:8888/dashboard?token=blah"

where the context of last_commit_log.txt is simply something like "WIP"

However, the actual contents of what i'm seeing is:

--------------------------0697da5b51372910
Content-Disposition: form-data; name="file"; filename="last_commit_log.txt"
Content-Type: text/plain WIP

when all i want to see is just the message "WIP"

This is my Java code:

Scanner scanner = new Scanner(request.getInputStream());
StringBuilder sb = new StringBuilder();
while (scanner.hasNextLine()) {
    sb.append(scanner.nextLine()).append("\n");
}
System.out.println(sb.toString());

where the request is the HttpRequest incoming

How do I only read the body of the request and strip the headers?

Savior
  • 3,225
  • 4
  • 24
  • 48
David T.
  • 22,301
  • 23
  • 71
  • 123
  • 1
    That is a multipart response. Those are part of the body. – Savior Apr 12 '16 at 03:57
  • i can modify it from either the curl side or from the java side. what do i do to read only the body of the multipart response? – David T. Apr 12 '16 at 03:58
  • 2
    Note that multipart means there are potentially multiple "bodies". You can either follow the HTTP specification and write a parser to extract the parts you need. Or use an appropriate HTTP client that does all this for you. Apache's Http Client comes to mind. – Savior Apr 12 '16 at 04:00
  • If you want to use cURL instead of a complete Java HTTP client, you can still use Apache HTTP client library to parse your cURL multipart response. See http://stackoverflow.com/questions/9261109/is-there-any-simple-http-response-parser-for-java – Samuel Apr 12 '16 at 04:18

0 Answers0