zebra13
zebra13

Reputation: 23

Inconsistency in Java Script and Python Result for encoding URLs

I am trying to programmatically generate URLs using Python. It works when using Java Script method ( encodeURIComponent ) but doesn't give same output when using Python ( urllib.urlencode )

This is the schema :

{
    "page": {
        "filters": {
            "power": {
                "keywordArray": [{
                    "keyword": "random word",
                    "isNegated": false
                }],
                "date": {
                    "from": "2019-04-22T08:00:00.000Z",
                    "to": "2019-05-22T08:00:00.000Z"
                }
            }
        }
    }
}

I successfully tried Java Script method encodeURIComponent using ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent ):

console.log(encodeURIComponent('({"page":{"filters":{"power":{"keywordArray":[{"keyword":"random word", "isNegated":False}],"date":{"from":"2019-04-22T08:00:00.000Z","to":"2019-05-22T08:00:00.000Z"}}}}})'));


(%7B%22page%22%3A%7B%22filters%22%3A%7B%22power%22%3A%7B%22keywordArray%22%3A%5B%7B%22keyword%22%3A%22random%20word%22%2C%20%22isNegated%22%3AFalse%7D%5D%2C%22date%22%3A%7B%22from%22%3A%222019-04-22T08%3A00%3A00.000Z%22%2C%22to%22%3A%222019-05-22T08%3A00%3A00.000Z%22%7D%7D%7D%7D%7D)

I am not able to generate same output using Python :

params = {
    "page": {
        "filters": {
            "power": {
                "keywordArray": [{
                    "keyword": "random word",
                    "isNegated": False
                }],
                "date": {
                    "from": "2019-04-22T08:00:00.000Z",
                    "to": "2019-05-22T08:00:00.000Z"
                }
            }
        }
    }
}
urllib.urlencode(params )

Output : 
'page=%7B%27filters%27%3A+%7B%27power%27%3A+%7B%27date%27%3A+%7B%27to%27%3A+%272019-05-22T08%3A00%3A00.000Z%27%2C+%27from%27%3A+%272019-04-22T08%3A00%3A00.000Z%27%7D%2C+%27keywordArray%27%3A+%5B%7B%27keyword%27%3A+%27random+word%27%2C+%27isNegated%27%3A+False%7D%5D%7D%7D%7D'

The output using Python is different than that using Java Script (which is correct )

Upvotes: 2

Views: 67

Answers (1)

Alberto Rivera
Alberto Rivera

Reputation: 3752

In javascript, you are encoding the whole object as a string for the url, while Python, urllib.urlencode "convert[s] a mapping object or a sequence of two-element tuples to a “percent-encoded” string". That is, the object passed will be interpreted as an url like

page => <restOfTheObject>

To have the same behaviour as in javascript, you should first convert the object to a json string, and then pass it to urllib.parse.quote

import urllib.parse
import json

params = {
    "page": {
        "filters": {
            "power": {
                "keywordArray": [{
                    "keyword": "random word",
                    "isNegated": False
                }],
                "date": {
                    "from": "2019-04-22T08:00:00.000Z",
                    "to": "2019-05-22T08:00:00.000Z"
                }
            }
        }
    }
}
urllib.parse.quote(json.dumps(params, separators=(',', ':')))

Upvotes: 1

Related Questions