Home > Software engineering >  Curl Requests Params inside url
Curl Requests Params inside url

Time:08-08

I have an API, where all important Parameter like ID, category is in the Request's URL and not as payload. What would be the smartest and recommended way to solve this?

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'

What I did till now

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

CodePudding user response:

requests.get has optional parameter params where you might deliver dict, consider following simple example

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

For further discussion read Passing Parameters In URLs

CodePudding user response:

The curl request that you posted in python would be:

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)

Insert your variables as required.

  • Related