Shop OBEX P1 Docs P2 Docs Learn Events
Parsing HTTP requests into a Map<String,String> — Parallax Forums

Parsing HTTP requests into a Map<String,String>

ChrisCantrellChrisCantrell Posts: 25
edited 2011-06-29 07:13 in Accessories
In my code I treat HTTP requests and responses as maps of string/string. A parser function takes the HTTP protocol apart and fills up a map structure. Then the data pieces can be accessed by name.

The request line becomes three entries in the map: "method", "url", and "http". Thus if you ask the map for the value of "method" you'll get "GET" or "POST" or what-not.

The HTTP header lines themselves are parsed into the map. Thus if you get the value for "Accept-Language" you will get what languages the browser user wants.

Any URL parameters are parsed into the map as well. Any POST parameters in an HTTP request are parsed into the map.

I added the map data structure to the OBEX here:
http://obex.parallax.com/objects/754/

The attached demo of the HTTP-to-map parser outputs to the PST. It uses a canned HTTP packet buffer captured from the wire. You can replace the output-string with the TCP driver's output string.

I find that treating the HTTP request as a map greatly simplifies my code. I can quickly access the pieces of data I want and ignore the things I don't. I hope this helps others too.
Sign In or Register to comment.