Click here to Skip to main content
15,867,937 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
For Example-
step1-For every api call we need to encrypt the payload with the help of encrypt API

step 2- This output of encrypt API will pass as payload of another API(say Login API)

Step 3- Again for another API say(logout API), I need to encrypt the plain text payload and use the encrypted payload as input payload for logout API

My approach is like
@pytest.fixture()
def setup():
    payload = {
        "userName": "7978130592",
        "password": "Test@123",
        "SessionId": "abc123",
        "version": "1620"
    }
url = "http://101.15.101.256:443/encrypt" 
response = requests.request("POST",url,headers=headers,data=json.dumps(payload))

python_response = json.loads(response.text)
jsonpath_data = parse("$.data")
data =(jsonpath_data.find(python_response)[0].value)
return data

def test_login(setup):

    headers = {
        'Content-Type': 'application/json',
            }
url = "https://testsite.com/users/Login"
    payload = setup

    response = requests.request("POST",url=url,headers=headers,data=payload)


and for logout API payload is like below and i want to encrypt this paylaod by using encrypt API which is defined as method for fixture.
{

"deviceId": "goldfishx86", 
"user": "xyz", 
"isGpsEnabled": "Y"
}


My query here is how can I replace my different payload again and again in same method so that I use the output in different API call

What I have tried:

Please help me, I tried but could not succeed to use different payload as input of fixture method

@pytest.fixture()
def setup():
    payload = {
        "userName": "7978130592",
        "password": "Test@123",
        "SessionId": "abc123",
        "version": "1620"
    }
url = "http://101.15.101.256:443/encrypt" 
response = requests.request("POST",url,headers=headers,data=json.dumps(payload))

python_response = json.loads(response.text)
jsonpath_data = parse("$.data")
data =(jsonpath_data.find(python_response)[0].value)
return data

def test_login(setup):

    headers = {
        'Content-Type': 'application/json',
            }
url = "https://testsite.com/users/Login"
    payload = setup

    response = requests.request("POST",url=url,headers=headers,data=payload)

def test_logout(setup):
payload = 
{

"deviceId": "goldfishx86", 
"user": "xyz", 
"isGpsEnabled": "Y"
}

url = "https://testsite.com/users/Logout"
response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
Posted
Updated 27-Feb-23 0:39am
v2

1 solution

It is not clear what your problem is but you probably need to do something like:
Python
data=json.dumps(payload)
encrypted = encrypt(data)
response = requests.request("POST", url, headers=headers, data=encrypted)

The encrypt function will most likely need other parameters which control the encryption process. You will also need a decrypt method for encrypted data returned from the remote site.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900