如何从响应中提取值并使用请求库将其与另一个值进行比较?
How to pull value from response and compare it with another value using requests library?
我正在向两个不同的 API 发出两个请求。
如何从响应中提取一个值,从第二个响应中提取另一个值并进行比较?
非常喜欢!
import requests
BASE_PATH = "https://example_url.com"
ACCESS_TOKEN = "1111"
def fill_basket():
basket_data = {
"article": "13",
"some_info1": "1"
"some_info2": "2"
}
r = requests.post(f"{BASE_PATH}/api/v5/basket/create/",
headers={"X-Access-Token": f"{ACCESS_TOKEN}"},
json=basket_data
)
assert r.status_code == 200, r.text
return r.json()["some_id"] #here i tried to catch value from the response 1
fill_basket()
def check_basket():
b = requests.post(f"{BASE_PATH}/api/v5/basket/calculate/",
headers={"X-Access-Token": f"{ACCESS_TOKEN}"}
)
assert b.status_code == 200, b.text
return b.json()["some_id"] #here i tried to catch value from the response 2
check_basket()
assert r.json()["some_id"] == b.json()["some_id"]
此处响应check_basket请求:
{
"quantity": 1,
"cost": 10,
"pricesCurrentCountry": {
"cost": 10,
"oldCost": 20,
"discount": 10
},
"productList": [
{
"some_id": "39KK8ME6BTEL27",
"article": "101",
}
]
}
此处对 fill_basket 请求的响应:
{
"some_id": "39KK8ME6BTEL27",
"quantity": 1,
"paymentMethodId": null,
}
根据您对“函数外的变量”的评论,此 Real Python article about scopes 可能会对您有所帮助。
由于您的函数正在返回值,您可以直接比较这些值(方法 2)或创建一个标识符来引用它们(方法 1)。
import requests
BASE_PATH = "https://example_url.com"
ACCESS_TOKEN = "1111"
def fill_basket():
basket_data = {
"article": "13",
"some_info1": "1", # A missing comma caused a syntax error here
"some_info2": "2"
}
r = requests.post(f"{BASE_PATH}/api/v5/basket/create/",
headers={"X-Access-Token": f"{ACCESS_TOKEN}"},
json=basket_data
)
assert r.status_code == 200, r.text
# here i tried to catch value from the response 1
return r.json()["some_id"]
def check_basket():
b = requests.post(f"{BASE_PATH}/api/v5/basket/calculate/",
headers={"X-Access-Token": f"{ACCESS_TOKEN}"}
)
assert b.status_code == 200, b.text
# here i tried to catch value from the response 2
return b.json()["productList"][0]["some_id"]
# Approach 1
# Follow this approach if you need to re-use the data.
x = fill_basket()
y = check_basket()
assert x == y
# Approach 2
# Or if you don't need to re-use the data, do this.
assert fill_basket() == check_basket()
我正在向两个不同的 API 发出两个请求。 如何从响应中提取一个值,从第二个响应中提取另一个值并进行比较? 非常喜欢!
import requests
BASE_PATH = "https://example_url.com"
ACCESS_TOKEN = "1111"
def fill_basket():
basket_data = {
"article": "13",
"some_info1": "1"
"some_info2": "2"
}
r = requests.post(f"{BASE_PATH}/api/v5/basket/create/",
headers={"X-Access-Token": f"{ACCESS_TOKEN}"},
json=basket_data
)
assert r.status_code == 200, r.text
return r.json()["some_id"] #here i tried to catch value from the response 1
fill_basket()
def check_basket():
b = requests.post(f"{BASE_PATH}/api/v5/basket/calculate/",
headers={"X-Access-Token": f"{ACCESS_TOKEN}"}
)
assert b.status_code == 200, b.text
return b.json()["some_id"] #here i tried to catch value from the response 2
check_basket()
assert r.json()["some_id"] == b.json()["some_id"]
此处响应check_basket请求:
{
"quantity": 1,
"cost": 10,
"pricesCurrentCountry": {
"cost": 10,
"oldCost": 20,
"discount": 10
},
"productList": [
{
"some_id": "39KK8ME6BTEL27",
"article": "101",
}
]
}
此处对 fill_basket 请求的响应:
{
"some_id": "39KK8ME6BTEL27",
"quantity": 1,
"paymentMethodId": null,
}
根据您对“函数外的变量”的评论,此 Real Python article about scopes 可能会对您有所帮助。
由于您的函数正在返回值,您可以直接比较这些值(方法 2)或创建一个标识符来引用它们(方法 1)。
import requests
BASE_PATH = "https://example_url.com"
ACCESS_TOKEN = "1111"
def fill_basket():
basket_data = {
"article": "13",
"some_info1": "1", # A missing comma caused a syntax error here
"some_info2": "2"
}
r = requests.post(f"{BASE_PATH}/api/v5/basket/create/",
headers={"X-Access-Token": f"{ACCESS_TOKEN}"},
json=basket_data
)
assert r.status_code == 200, r.text
# here i tried to catch value from the response 1
return r.json()["some_id"]
def check_basket():
b = requests.post(f"{BASE_PATH}/api/v5/basket/calculate/",
headers={"X-Access-Token": f"{ACCESS_TOKEN}"}
)
assert b.status_code == 200, b.text
# here i tried to catch value from the response 2
return b.json()["productList"][0]["some_id"]
# Approach 1
# Follow this approach if you need to re-use the data.
x = fill_basket()
y = check_basket()
assert x == y
# Approach 2
# Or if you don't need to re-use the data, do this.
assert fill_basket() == check_basket()