Search code examples
pythonpython-3.xcherrypy

CherryPy passing URL with query params as single parameter in POST


I have a Python 3.4 CherryPy web application with a POST that does some processing on a YouTube URL.

POST localhost:8080/api/url=https://www.youtube.com/watch?v=WS6-vI70oc0

If I use a YouTube URL with query params, e.g.:

POST localhost:8080/api/url=https://www.youtube.com/watch?v=WS6-vI70oc0&list=RDGMEM_v2KDBP3d4f8uT-ilrs8fQVMWS6-vI70oc0

CherryPy treats this query param (&list) as a query param to my POST API. Output from my API:

def POST(self, youtube_url=None, **kwargs):
    print('YOUTUBE URL: %s'%youtube_url)
    print('kwargs: %s'%kwargs)

output:

YOUTUBE URL: https://www.youtube.com/watch?v=WS6-vI70oc0
kwargs: {'list': 'RDGMEM_v2KDBP3d4f8uT-ilrs8fQVMWS6-vI70oc0"'}

However I would like the entire string to be treated as the youtube_url, without CherryPy automatically separating the ampersand sections into query params.

From the Javascript side I tried encodeURI and surrounding the entire URL with "", which didn't change CherryPy's behavior.

Javascript/HTML side:

//input box in HTML:
<input type="text" class="form-control" id="youtubeUrlString" value="https://www.youtube.com/watch?v=2XNEmxl2rYs" style="height: 34px">

//get input box value in javascript and post
var urlString = document.getElementById('youtubeUrlString').value;
req.open('POST', api_url + '?youtube_url=' + encodeURI(urlString) + '"');

Solution

  • urlString variable is as a url component so you should encode it with encodeURIComponent instead of encodeURI. This SO answer answers the difference of them and when to use them.