Question

I am trying to pass a URL to a variable in python (youtube url of the video to be played on the Raspberry Pi), but somewhere along the way the forward slash character is being interpreted as an end of the string/variable. So instead of getting "http://www.youtube.com/watch?v=5NV6Rdv1a3I", I get "http:".

I am using the WebIOPi server to display a webpage in html that contains a textarea. When I click a button on the webpage, the function sendLink() is called and the text from the textarea passed as an argument.

Content of index.html:

    function sendLink() {
            var text = $('textarea#videolink').text();
            webiopi().callMacro("playVideo", text);
    }

    ...

    <textarea rows="1" cols="30" id="videolink">Enter YouTube link here</textarea>

The function callMacro calls a macro named playVideo, written in another script in python:

@webiopi.macro
def playVideo(text):
    print (text)
    webiopi.debug(text)

When I enter "a/b/c" into the textarea and click the button, only "a" is displayed by print and webiopi.debug, even though the general debug information that is displayed along with it says "POST /macros/playVideo/a/b/c HTTP/1.1" 200, which I believe means that the variable is being passed to the function correctly.

(Idea for sending text entered into the textarea taken form here: http://timcorrigan.com/raspberry-pi-tracked-robot-streaming-video-and-text-to-speech/)

How do I solve this? Any solution is appreciated.

Was it helpful?

Solution 2

I solved the problem. While kalhartt's answer did not completely apply to my problem, this part got me thinking:

Your best option is probably to escape the string in javascript before sending it and unescape it in your macro. Looks like you will need to escape all url unsafe characters, /, and ,.

As I wrote in my original question, WebIOPi calls macros like this

    POST /macros/playVideo/a/b/c HTTP/1.1

where "playVideo" is the macro that is being called and anything here

    /macros/playVideo/argument/

is an argument to be passed to the macro. The argument is only up to the forward slash after it, because after that slash there are no more categories (you used one slash to say you were going to call a macro, another slash to say the name of the macro, the third slash to pass arguments, and the fourth one for... what?).

This is why anything after a forward slash was being cut off. I solved the issue by replacing all forward slashes in javascript with

     text = text.replace(/\//g,"fslash");

and then replacing "fslash" with "/" in the python script that was supposed to receive the argument. I also replaced question marks, since those were causing problems as well.

OTHER TIPS

Relevant source is do_POST on line 180 here.

Specifically notice:

paths = relativePath.split("/")
if len(paths) > 2:
    value = paths[2]

Where value is eventually passed to your macro. It shows that anything past the second / is discarded. As an aside, it appears you can comma separate arguments in the url and they will be passed positionally.

Your best option is probably to escape the string in javascript before sending it and unescape it in your macro. Looks like you will need to escape all url unsafe characters, /, and ,.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top