-1

I would like to have generic way of parsing http request line:

GET /foo?bar=baz HTTP/1.1

would be parsed into object with properties: path, query parameters.

I know that I could do it manually but I wonder if there is a library for that. There is bunch od libraries parsing urls but is there any parsing http requests?

pixel
  • 24,905
  • 36
  • 149
  • 251
  • 1
    I think OP don't wants to use a whole web server library. He only asks for a library that parses the request, he wants to handle the request by himself. However I also found many topics at SO for the same question but no really satisfying answer. Depending on the complexity of your problem I would just use a simple regex with some capturing groups. Something like `([A-Z]) /(.+) HTTP/([\d\.])` or so, probably needs some finetuning. For extracting the query parameters I think there are some nice libraries but I also found this https://stackoverflow.com/a/11733697/2411243 – Zabuzard Jun 18 '17 at 17:10
  • @Frederik.L I don't need a http client, I just want to parse request for wiremock matcher – pixel Jun 18 '17 at 17:11
  • Sorry, I thought it was about a lib to build a request and parse the *results* of an http request. – Frederik.L Jun 18 '17 at 17:13
  • @Frederik.L No problem, upvotes much obliged - since it would be hard to get an answer with `-1` – pixel Jun 18 '17 at 17:22
  • I have a simple library for that: https://github.com/rollback-dropdev/jrestclient There are some other libraries which do basically the same but give it a try, take a look in the examples – Felix Jun 18 '17 at 17:22
  • @rollback I don't need a http client, I just want to parse request for wiremock matcher – pixel Jun 18 '17 at 17:36
  • @pixel This probably goes very near of what you need http://www.java2s.com/Code/Java/Network-Protocol/HttpParser.htm and using a lib for parsing a line can be overkill in a way. – Frederik.L Jun 18 '17 at 18:46

1 Answers1

0

I've end up with extracting path="/foo?bar=baz" and creating new URL("http", "localhost", path)

pixel
  • 24,905
  • 36
  • 149
  • 251