简体   繁体   中英

How can i capture result of requests.POST in Python?

I am quite new to Python and was trying to simulate the result captured when testing API POST requests in Postman. I am basically trying to capture the token generated using the Post method.

I have the following details in Postman which gives me the token details.

Authorization Tab: Type: Basic Auth, Username , Password

Headers Tab: Content-Type : application/x-www-form-urlencoded, Authorization: #This is Auto-Filled

Body Tab : raw: grant_type=client_credentials&scope=read_snfa_unifiedgateway

Now when i run with above details in Postman, it gives me access token in the result but when i use the same details using the following Python Code, i get only <Response [200]> as result for response variable.

import requests
url = 'https://appurl.com/oauth/ls/connect/token'
body = 'grant_type=client_credentials&scope=read_snfa_unifiedgateway'
response = requests.post(
        url,
        body, 
        auth=HTTPBasicAuth('my_username', 'my_password'), headers={'Content-Type': 'application/x-www-form-urlencoded'}
        )

How can i change the above Python code in such a way that i could get the result in the format i get in Postman as following and then i could use the access_token value in next operation for another POST Method?

{
    "access_token": "eyJ0eXAiOiJKV1QiLCJhbsdfsdfsdfsdfcsdcsdvarglkwelkrmgmasdmnfjtugnasmdfisdfmmmmpeirhg_-e60s72zgV8Gn2hUiWwlelNQhJongUW6fxwD42c1N5u4R2JJdrj5V_bIwnvY_C_l5wHlIFSQRE1E-5KzP7WG9XjmV9oXRXXGjNhwRqEocGdiEMjcibyiYQNZmG2h-GbsTKvCc21hRNhyRF_y4mdwVUytAXT68TuwZxsTbjUzEvPdwd2JZaFnD9Elo7akSk2ROnRMxN70fsoLzEK71kbzoYkti1jX_V8i_s6K0wmLma-x4nc2kW5mpFM0R9NPqH-kGf-4ZUKim03frifpsGl6Nqo-eN5oZ-b6YgK56mSjxpxQ",
    "expires_in": 3600,
    "token_type": "Bearer"
}

Use response.json() to get result in dict format. You can use

access_token = response.json()['access_token']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM