URL中的Curl请求参数

lf5gs5x2  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(189)

我有一个API,其中所有重要的参数,如ID,类别是在请求的URL,而不是作为有效负载。什么是最聪明的和建议的方式来解决这个问题?

curl --request GET \
  --url 'https://api.otto.market/v2/products?sku=SOME_STRING_VALUE&productReference=SOME_STRING_VALUE&category=SOME_STRING_VALUE&brand=SOME_STRING_VALUE&page=SOME_INTEGER_VALUE&limit=SOME_INTEGER_VALUE' \
  --header 'Authorization: Bearer REPLACE_BEARER_TOKEN'

我现在所做的

def example(content_type: str, cache_control: str, grand_type:str, client_id:str):

    endpoint = website.BASE_URL
    header_content = {
        'Content-Type': (f'{content_type}'),
        'Cache-Control': (f'{cache_control}')
    }
    data_content = {
        'grant_type': (f'{grand_type}'),
        'client_id': (f'{client_id}')
    }
    response = requests.get(url= endpoint, headers=header_content, data=data_content, verify=False)
    return response
ecfsfe2w

ecfsfe2w1#

requests.get具有可选参数params,您可以在其中传递dict,请考虑以下简单示例

import requests
parameters = {"client":"123","product":"abc","category":"a"}
r = requests.get("https://www.example.com",params=parameters)
print(r.url)  # https://www.example.com/?client=123&product=abc&category=a

有关详细讨论,请参阅在URL中传递参数

dm7nw8vv

dm7nw8vv2#

您在python中发布的curl请求将是:

import requests

headers = {
    'Authorization': 'Bearer REPLACE_BEARER_TOKEN',
}

params = {
    'sku': 'SOME_STRING_VALUE',
    'productReference': 'SOME_STRING_VALUE',
    'category': 'SOME_STRING_VALUE',
    'brand': 'SOME_STRING_VALUE',
    'page': 'SOME_INTEGER_VALUE',
    'limit': 'SOME_INTEGER_VALUE',
}

response = requests.get('https://api.otto.market/v2/products', params=params, headers=headers)

根据需要插入变量。

相关问题