'Python: Replace Python text - CURL Python

I need the variable defined as NUEMRODEDNI to be tempered, how is it done?

Excuse me I'm a newbie

I don't know how to use a variable defined in python, I need it to be replaced as shown in the image

import string

import requests

from requests.structures import CaseInsensitiveDict



url = "www.url.com:8022/SistemaIdentificacion/servlet/com.personas.consultapersona?030bf8cfcd4bfccbd543df61b1b43f67,gx-no-cache=1648440596691"



NUEMRODEDNI = "41087712"



headers = CaseInsensitiveDict()

headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0"

headers["Accept"] = "*/*"

headers["Accept-Language"] = "es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3"

headers["Accept-Encoding"] = "gzip, deflate"

headers["GxAjaxRequest"] = "1"

headers["Content-Type"] = "application/json"

headers["AJAX_SECURITY_TOKEN"] = "a6da9873adb..."

headers["X-GXAUTH-TOKEN"] = "eyJ0eXAiOiJ..."

headers["Origin"] = "http://www.url.com"

headers["Connection"] = "keep-alive"

headers["Referer"] = "www.url.com/SistemaIdentificacion/servlet/com.personas.consultapersona"

headers["Cookie"] = "GX_CLIENT_ID=0496f100-9e4e-4e36-a68d-ba3770ee2bff; GX_SESSION_ID=KUqyHU%2FZbpu96sYlj7Gry8bCYpV6CaSgVk0BLxVCpAU%3D; JSESSIONID=1812E6AC00940BDB325EF9592CB93FF8; GxTZOffset=America/Argentina/Buenos_Aires"



data = '{"MPage":false,"cmpCtx":"","parms":[EDIT HERE,{"s":"M","v":[["","(None)"],["F","Femenino"],["M","Masculino"]]},"M",{"User":"","CompanyCode":0,"Profile":"","UsrOneLoginID":"6647","Depid":1,"UsrLP":6488,"unidad":"","unidadid":"68","IMFParteCuerpo":"","denunciasid":0,"destino":"68","TipoPersona":"","NombreArchivo":"","denorigen":"","macdestinoscodorganigrama":""}],"hsh":[],"objClass":"consultapersona","pkgName":"com.personas","events":["ENTER"],"grids":{}}'





resp = requests.post(url, headers=headers, data=data)



print(resp.content)


Solution 1:[1]

Please provide your code typed out rather than a screenshot of it, so that we can simply copy & run it on our end.

Nontheless you should try:

NUMERODNI = "41087712"
data = '{"MPage":false,"cmpCtx":"","parms":[EDIT HERE,{"s":"M","v":[["","(None)"],["F","Femenino"],["M","Masculino"]]},"M",{"User":"","CompanyCode":0,"Profile":"","UsrOneLoginID":"6647","Depid":1,"UsrLP":6488,"unidad":"","unidadid":"68","IMFParteCuerpo":"","denunciasid":0,"destino":"68","TipoPersona":"","NombreArchivo":"","denorigen":"","macdestinoscodorganigrama":""}],"hsh":[],"objClass":"consultapersona","pkgName":"com.personas","events":["ENTER"],"grids":{}}'

data = data.replace("EDIT HERE", NUMERODNI)

print(data) # '{... "parms":[41087712, ...}'

This solution definetly delivers the desired string as result. If your code still does not do what you would like it to, then the actual issue has to be somewhere else.

Solution 2:[2]

Since you're POSTing JSON data, it's easier to just keep your data as a Python dict and tell Requests to JSON-ify it (requests.post(json=...)).

That way you don't need string substitution, just use the variable.

I also took the opportunity to make your headers construction shorter – it can just be a dict.

import requests

url = "www.url.com:8022/SistemaIdentificacion/servlet/com.personas.consultapersona?030bf8cfcd4bfccbd543df61b1b43f67,gx-no-cache=1648440596691"

NUEMRODEDNI = "41087712"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0",
    "Accept": "*/*",
    "Accept-Language": "es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3",
    "Accept-Encoding": "gzip, deflate",
    "GxAjaxRequest": "1",
    "Content-Type": "application/json",
    "AJAX_SECURITY_TOKEN": "a6da9873adb...",
    "X-GXAUTH-TOKEN": "eyJ0eXAiOiJ...",
    "Origin": "http://www.url.com",
    "Connection": "keep-alive",
    "Referer": "www.url.com/SistemaIdentificacion/servlet/com.personas.consultapersona",
    "Cookie": "GX_CLIENT_ID=0496f100-9e4e-4e36-a68d-ba3770ee2bff; GX_SESSION_ID=KUqyHU%2FZbpu96sYlj7Gry8bCYpV6CaSgVk0BLxVCpAU%3D; JSESSIONID=1812E6AC00940BDB325EF9592CB93FF8; GxTZOffset=America/Argentina/Buenos_Aires",
}

data = {
    "MPage": False,
    "cmpCtx": "",
    "parms": [
        NUEMRODEDNI,
        {"s": "M", "v": [["", "(None)"], ["F", "Femenino"], ["M", "Masculino"]]},
        "M",
        {
            "User": "",
            "CompanyCode": 0,
            "Profile": "",
            "UsrOneLoginID": "6647",
            "Depid": 1,
            "UsrLP": 6488,
            "unidad": "",
            "unidadid": "68",
            "IMFParteCuerpo": "",
            "denunciasid": 0,
            "destino": "68",
            "TipoPersona": "",
            "NombreArchivo": "",
            "denorigen": "",
            "macdestinoscodorganigrama": "",
        },
    ],
    "hsh": [],
    "objClass": "consultapersona",
    "pkgName": "com.personas",
    "events": ["ENTER"],
    "grids": {},
}

resp = requests.post(url, headers=headers, json=data)
resp.raise_for_status()
print(resp.content)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 AKX