Difference between request.body and request.params

In the context of HTTP requests, the params and body represent different parts of the request data.

params

Query parameters are key-value pairs added to the URL of a request.

Query parameters are commonly seen in GET requests and are appended to the URL after a question mark (?). For example,
https://api.example.com/users?name=John&age=25. Query parameters are typically used for filtering, sorting, or paginating data.

async def _get(self, endpoint) -> dict:
        params    = {
    
    
        "symbol": "BTCUSDT",
        "limit": 100
    }
        async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session:
            async with session.get(f"{
      
      self.API_URL}/{
      
      endpoint}", params=params) as response:
                print(f"{
      
      self.API_URL}/{
      
      endpoint}")

body

The request body contains additional data sent along with the requestm typically used in POST\PUT\PATCH requests. The request body is separate from the URL and is sent as part of the HTTP request after the headers.

You can pass any string (special characters) in the body, while encoding them in the url would get you vulnerable to status 414 (Request-URI Too Long).

async def _get(self, endpoint) -> dict:
        data    = {
    
    
        "symbol": "BTCUSDT",
        "limit": 100
    }
        async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session:
            async with session.get(f"{
      
      self.API_URL}/{
      
      endpoint}", data=json.dumps(data)) as response:
                print(f"{
      
      self.API_URL}/{
      
      endpoint}")

summary

It’s important to note that the choice between query parameters (params) and request body (body) depends on the API design and the specific requirements of the API you’re interacting with. Some APIs may expect certain parameters in the query string, while others may require the parameters to be included in the request body. Always refer to the API documentation to determine how to structure your requests correctly.

猜你喜欢

转载自blog.csdn.net/majiayu000/article/details/131089254