Can't get JavaScript and Python strings to be the same because of backslash escaping

Viewed 80

I'm trying to make a POST web request work for 24 hours now. But something is messing up my body string. I have to call a JavaScript function within a Python Program (to run the encryption function which is used by the webiste I'm trying to request) but the string processed by JavaScript isn't the same as the string returned by JavaScript (using js2py library for executing JavaScript within Python).

I have an object like this one:

let enc = {
    data: {
        "endpointId": 17273,
         params: {
            "DEBUG_": false,
            "userId": String(userId),
            "userIsOnline": true,
            "lang": "en",
            "locale": "en#US"
    }
}

The final body for the POST request must look like this (and exactly like this because on the server side the data will be decrypted by using the same key as I use within the JavaScript function):

{"endpointId":17273,"params":"{\"DEBUG_\":false,\"userId\":\"338de884bc1f41818dfae7a8b19f5613\",\"userIsOnline\":false,\"lang\":\"en\",\"locale\":\"en#US\"}"}

I don't know how the put just one backslash (like "DEBUG_") everything I'm trying it will be two or three backslashes (and the furthermore the backslashes should be only within the params object, not at the appId).

And another problem is that if I return the string to python console.log(n.data) is not matching return n.data:

console.log("Encrypting: " + n.data);
> Encrypting: {"endpointId":17273,"params":"{\\"DEBUG_\\":false,\\...

(as you can see two backslashes here!)

JavaScript:

return  n.data

Python:

body = js2py.eval_js(Constants.sign_function + f"make('{input}');");
print(f"#  Returned: {body}")
Returned: {"endpointId":17273,"params":"{\"DEBUG_\":false,\\..

And the problem here is now if I send the web request with the returned body like:

req = requests.post(url, data=body, headers=headers)

The response will be 403 forbidden because the sent data is not the encrypted data (because both differ in backslashes within the string)

I tried a lot of things so far (build the body from a string, build the body from dict, build the body within JavaScript, build the body within Python...) but can't make it work. The backslashes always kill my efforts in the end.

0 Answers
Related