0

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) + '"');
Sevag
  • 191
  • 2
  • 15
  • can't you just base64 encode it in the url with [btoa](https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/btoa)? And the use [this](https://docs.python.org/2/library/base64.html) in python to decode it. – RickyA Sep 28 '15 at 15:22
  • Are you sure that you are using POST? Can I see the javscript code that it's posting this? You mention that you were using the `encodeURI` function. – cyraxjoe Sep 28 '15 at 17:55
  • I will edit the question with the Javascript code. @RickyA I will try that, thanks. – Sevag Sep 29 '15 at 09:08
  • You may also need to uri encode it before sending it. I am not sure if it will produce any non valid characters in the encoding. – RickyA Sep 29 '15 at 11:12

1 Answers1

0

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.

Community
  • 1
  • 1
Ozan
  • 1,044
  • 1
  • 9
  • 23
  • Both btoa and encodeURIComponent work, but with btoa I need code on the Python side to handle it so I prefer this solution. – Sevag Sep 30 '15 at 00:18