cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
126
Views
0
Helpful
0
Replies

How to convert string with spaces to URL encoding in EEM scripting!

memschweiler
Level 1
Level 1

Greetings all!

I recently had a need to pass along data to a running python script in a Docker container on a router. It performs an API call to the python script, passing along a GET request with the necessary variables via a "more" command to the formulated URL request from an EEM script that is triggered on a VG when a call traverses a monitored dial peer

The challenge I had was that the caller name could have spaces in it and I needed to codify that to convert the spaces to %20 before passing along the value to the URL. After doing some digging, getting suggestions from different forums and much testing, I was able to come up with a relatively simple solution via EEM scripting and didn't have to go the TCL route for this solution. Given the caller name being something like "The Caller", that would be captured from the active call and saved in a variable that we would call callerName

I created an event manager variable like so for the HTML space code:

 

event manager environment SpaceCode %20
 
The process to convert things:
 
action 012 comment Process the Caller Name to replace spaces with %20 for URL encoding
action 013 foreach word "$callerName"
action 014 append newName "$word$SpaceCode"
action 015 end
action 016 comment Trim the trailing %20 from the modified Caller Name
action 017 string trimright "$newName" "$SpaceCode"
action 018 comment Store the final modified Caller Name in a variable
action 019 set modifiedCallerName $_string_result
 
The final result will be "The%20Caller", stored in the new modifiedCallerName variable that can then be included in the URL call for the API
 
The above loop will always add the %20 to the end so that's why I trim it off after the loop has completed, although you could leave that in there and deal with it later by trimming the passed variable if you like. I preferred to clean it up before I passed it along to my Python script running in the Docker container.
 
This works very nicely without having to involve TCL. I also wanted to avoid TCL because if there is any error encountered with a TCL script that is attached as a service to a dial peer, the call will fail. We couldn't have that, so if something goes wrong in the EEM processing, at least the call is still successful even if the entire EEM script doesn't finish processing
 
Hopefully this helps someone else that is looking for a similar solution.  Enjoy!
 
 
0 Replies 0