ESP32 MicroPython: Developing a simple URL query string parser

The objective of this post is to explain how to develop a very simple URL query string parser using MicroPython. The tests shown through this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

 

Introduction

The objective of this post is to explain how to develop a very simple URL query string parser using MicroPython. Explaining what a query string is is outside the scope of this post, but you can read more about it here.

Our query string parser will be very simple and thus we will assume that the query string has a well behaved format and thus all the parameter value pairs are separated from each by a “&” character and each parameter is separated from its value by a “=” character. We will also assume that each parameter will always have a corresponding value.

The tests shown through this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The IDE used was uPyCraft.


The code

Since we want to develop a reusable generic solution, we will encapsulate our code in a function. Naturally, this function will have an input variable so we can pass the URL query string to be processed. We will name our function qs_parse.

def qs_parse(qs):
## Function code

Inside the function, we will start by declaring an empty dictionary, which maps well to the query string “parameter=value” structure. Thus, we will by able to access each parameter by its name, since the parameter names will be used as the dictionary keys. You can learn more about dictionaries in this previous post.

parameters = {}

As stated in the previous section, we know that each parameter value pair is separated by an ampersand. So, if we use the “&” character as separator, we can isolate each parameter value pair.

To do it, we can use the string split method, which receives as input a string that is used as separator and returns a list of sub-strings resulting from the separation. The separator is not included in the results and thus we will get a clean list with parameter value pairs in each sub-string.

Note that since the split function is a string method, we call it on the string variable that contains the query parameters. In our case, that string is the input argument of the qs_parse function, which we called qs.

ampersandSplit = qs.split("&")

Since we are developing a generic parsing function, we will assume that we don’t know how many parameters exist on our query. Thus, we will iterate the previously obtained list element by element, using a for … in loop.

for element in ampersandSplit:
#iteration code

So, in each iteration of the loop, the element variable will have a string composed by each parameter value pair, in the “parameter=value” format. Since we know that the parameter and the value are separated by the equal character, we can again apply the split function, using the “=” character as separator.

equalSplit = element.split("=")

Since we are iterating pair by pair, we know that the output of this operation will always be a list with two positions. The first position will have the name of the parameter (it is the sub-string left of the separator character) and the second position will have the value (it is the sub-string right of the separator character).

Taking this into account, we simply map the first element of the resulting list to a key of the dictionary and the second  to the value of the dictionary. Remember that MicroPython indexes are zero based and thus the list first and second elements are in indexes 0 and 1, respectively.

parameters[equalSplit[0]] = equalSplit[1]

To finalize the code, we simply end the function by returning our dictionary, which is stored on the parameters variable. The final source code can be seen below.

def qs_parse(qs):

  parameters = {}

  ampersandSplit = qs.split("&")

  for element in ampersandSplit:

    equalSplit = element.split("=")

    parameters[equalSplit[0]] = equalSplit[1]

  return parameters


Testing the code

To test the code, simply upload it to your ESP32. In my case, I’m using uPyCraft and thus it will create a .py file with the specified name.

I will call the file qs_parse (I’ve used the same name as the function, but it could have been different) and thus I will later need to import it as a module to be able to use the developed function. Upon uploading, to test everything, we can use the following code.

import qs_parse

stringToParse = "param1=val1&param2=val2&param3=val3"
parameters = qs_parse.qs_parse(stringToParse)

print(parameters)

Just as a quick analysis, we start by importing the module where we encapsulated our function. Then we declare a string matching an example of an URL query string. Finally, we call the qs_parse function of our module (remember that both the module and the function have the same name) and print the result. You can check the output at figure 1.

ESP32 MicroPython simple URL query string parser.png

Figure 1 – Result of applying the query string parser.

As can be seen, the output dictionary is composed of keys and values that match the URL parameters. After this, we can simply use the dictionary functions to check which keys and values are available.

Advertisements
This entry was posted in ESP32 and tagged , , , , , , , , , . Bookmark the permalink.

One Response to ESP32 MicroPython: Developing a simple URL query string parser

  1. Pingback: ESP32 MicroPython: Getting the query parameters on a Picoweb app | techtutorialsx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s