0

I'm trying to extract some information from raw HTTP Request messages (like the one below) and store them into instances of the org.apache.http.message.BasicHttpRequest (https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/index.html) class.

I was able to employ org.apache.http.message.BasicLineParser class and its method parseHeader(String value, LineParser parser) to process (parse + store) the headers, but I don't know how to deal with the parameters passed with the form.

POST https://gist.github.com:443/gists HTTP/1.1
Host: gist.github.com
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en,fr;q=0.8,it-it;q=0.6,it;q=0.4,en-us;q=0.2
Accept-Encoding: gzip, deflate
DNT: 1
Referer: https://gist.github.com/
Connection: keep-alive
Content-Type: multipart/form-data; boundary=-------------------------10314229756379422411656878663
Content-Length: 1242

-----------------------------10314229756379422411656878663
Content-Disposition: form-data; name="parameter_name"

parameter_value

Do you know an utility which can parse the content starting after the header above (i.e. after the first empty line)? What I am interest in collecting are all the pairs <"parameter_name","parameter_value"> present in the request body.

I have already read similar answers/questions such as Parsing raw HTTP Request but I did not find anything helpful on the form-data component

Thanks in advance for your help and time

Community
  • 1
  • 1
Alby
  • 13
  • 6

1 Answers1

0

What you are seeing is MIME encoded content body. HttpClient is content agnostic and therefore does not provide a means of parsing such content. One can however use Apache Mime4J to do so.

ok2c
  • 26,450
  • 5
  • 63
  • 71
  • I tried to do so, but it appears I have to define the content handler, which is not a simple task. I hoped for something more similar to what httpclient provides for parsing headers – Alby Aug 21 '14 at 15:11
  • @Alby: Mime4J supports both SAX style (event driven) and DOM style (object model) parsing. You can use DefaultMessageBuilder#parseMessage to parse mime content into an object model – ok2c Aug 21 '14 at 19:34
  • Thanks a lot! (I still cannot vote up, but I accepted your answer). I was able to get the parameter_name with `DefaultMessageBuilder dmb = new DefaultMessageBuilder(); AbstractMessage am = (AbstractMessage) dmb.parseMessage(instream); Multipart mp = (Multipart) am.getBody(); List es = mp.getBodyParts(); for (int j = 0; j < es.size(); j++) { System.out.println(es.get(j).getHeader().toString()); }` – Alby Aug 22 '14 at 09:30