Hướng dẫn python requests query params multiple values - python yêu cầu truy vấn tham số nhiều giá trị

Tôi đang sử dụng thư viện yêu cầu của Python để thực hiện 'nhận' từ API. Đây là một phần của mã của tôi:

Show
payload = { 'topicIds':'128487',
            'topicIds':'128485', 
        'topicIds': '242793',
            'timePeriod':'10d', }

r= requests.get(url, params=payload, headers=headers)

Theo tài liệu API, chúng tôi có thể gán nhiều chủ đề cho một yêu cầu như sau:

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
5

Khi tôi cố gắng đặt giá trị chủ đề làm danh sách như thế này:

payload = { 'topicIds':['128487' , '242793'],

Tôi gặp lỗi:

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
6

Tuy nhiên, khi tôi chạy mã, tôi chỉ nhận được dữ liệu từ chủ đề cuối cùng => 'TopicIds': '242793' Tôi có viết sai từ điển tải trọng không?

Thanks,

Trong các ví dụ này, bạn đã thấy cách khai báo xác nhận cho các giá trị

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
9.

Xem các chương tiếp theo để xem cách khai báo xác nhận cho các loại khác, như số.

Với các tham số truy vấn, nhiều giá trị có thể được biểu diễn là

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
0 hoặc
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
1. Điều này sẽ phù hợp với những gì các ứng dụng nhận được các yêu cầu sẽ chấp nhận. RFC 3986 không có định nghĩa về cách cư xử khi cùng một tham số có nhiều giá trị. Nó không khuyến nghị một tùy chọn này hay lựa chọn khác.

Điều này có nghĩa là hành vi của các tham số truy vấn sẽ được xác định bởi API - chúng có thể chấp nhận

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
0,
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
1 hoặc cả hai. Đó là phía tây hoang dã của HTTP. Nó cũng có thể được xác định bởi khung cung cấp năng lượng cho API trong câu hỏi, có thể xác định cách nó hỗ trợ nhiều tham số. Ví dụ, trong Python, Django đã hỗ trợ tích hợp để xử lý việc này thông qua
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
4 khi ở trong bình, có một nhu cầu sửa đổi cách xử lý các tham số để các tham số được phân tích cú pháp một cách thích hợp.

Theo sở thích cá nhân, tôi sẽ sử dụng nhiều cặp

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
5 vì nó trở nên dễ đọc hơn khi đọc yêu cầu. Nếu khung hỗ trợ nó, thực tế là nó trở nên dễ đọc hơn có thể quan trọng hơn trong khi khắc phục các vấn đề với các yêu cầu như thế này.

Các trường biểu mẫu ..

Yêu cầu cơ thể ..

Thông số dữ liệu định tuyến ..

Tham số chuỗi truy vấn .. allows you to declare additional information and validation for your parameters.

Đã tải lên các tệp ..

Python 3.6 trở lên

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

Và A

payload = { 'topicIds':['128487' , '242793'],
31:

Ghi chú

Thông số bí danh

Hãy tưởng tượng rằng bạn muốn tham số là

payload = { 'topicIds':['128487' , '242793'],
32.

Giống như trong:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
8its length doesn't exceed 50 characters.

Nhưng payload = { 'topicIds':['128487' , '242793'], 32 không phải là một tên biến Python hợp lệ.

Gần nhất sẽ là

payload = { 'topicIds':['128487' , '242793'],
34.

Python 3.6 trở lên

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

Và A payload = { 'topicIds':['128487' , '242793'], 31:

Thông số bí danh

Python 3.6 trở lên

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

Vì chúng ta phải thay thế giá trị mặc định

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
0 trong hàm bằng
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
3, giờ đây chúng ta có thể đặt giá trị mặc định với tham số
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
4, nó phục vụ cùng mục đích xác định giá trị mặc định đó.

So:

q: Union[str, None] = Query(default=None)

... Làm cho tham số tùy chọn, giống như:

q: Union[str, None] = None

Và trong Python 3.10 trở lên:

payload = { 'topicIds':['128487' , '242793'],
0

... Làm cho tham số tùy chọn, giống như:

Và trong Python 3.10 trở lên:

Nhưng nó tuyên bố nó rõ ràng là một tham số truy vấn.

Thông tin

Hãy nhớ rằng phần quan trọng nhất để tạo một tham số tùy chọn là phần:

hoặc là:not required.

Vì nó sẽ sử dụng

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
0 làm giá trị mặc định và theo cách đó làm cho tham số không bắt buộc.

Phần

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
7 cho phép trình chỉnh sửa của bạn cung cấp hỗ trợ tốt hơn, nhưng đó không phải là điều cho Fastapi biết rằng tham số này không bắt buộc.

payload = { 'topicIds':['128487' , '242793'],
1

Sau đó, chúng ta có thể chuyển nhiều tham số hơn cho

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
7. Trong trường hợp này, tham số
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
1 áp dụng cho các chuỗi:

Điều này sẽ xác thực dữ liệu, hiển thị lỗi rõ ràng khi dữ liệu không hợp lệ và ghi lại tham số trong thao tác đường dẫn lược đồ OpenAPI.

Thêm xác nhận thêm

Bạn cũng có thể thêm một tham số

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
9:

payload = { 'topicIds':['128487' , '242793'],
2

payload = { 'topicIds':['128487' , '242793'],
3

Python 3.6 trở lên

Thêm biểu thức chính quy

Bạn cũng có thể thêm một tham số

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
9:

payload = { 'topicIds':['128487' , '242793'],
4

payload = { 'topicIds':['128487' , '242793'],
5

Python 3.6 trở lên

  • Thêm biểu thức chính quy
  • Bạn có thể xác định một biểu thức thông thường mà tham số sẽ khớp:
  • Biểu thức chính quy cụ thể này kiểm tra xem giá trị tham số nhận được:

q: Union[str, None] = Query(default=None)
0: Bắt đầu với các ký tự sau, không có ký tự trước đây."regular expression" ideas, don't worry. They are a hard topic for many people. You can still do a lot of stuff without needing regular expressions yet.

q: Union[str, None] = Query(default=None)
1: có giá trị chính xác
q: Union[str, None] = Query(default=None)
1.FastAPI.

q: Union[str, None] = Query(default=None) 3: Kết thúc ở đó, không có thêm bất kỳ ký tự nào sau q: Union[str, None] = Query(default=None) 1.

Nếu bạn cảm thấy lạc lõng với tất cả những ý tưởng "biểu hiện chính quy" này, đừng lo lắng. Họ là một chủ đề khó khăn cho nhiều người. Bạn vẫn có thể làm rất nhiều thứ mà không cần biểu cảm thường xuyên.

Nhưng bất cứ khi nào bạn cần chúng và đi và tìm hiểu chúng, hãy biết rằng bạn đã có thể sử dụng chúng trực tiếp trong Fastapi.

payload = { 'topicIds':['128487' , '242793'],
6

Giá trị mặc định Jo

Theo cách tương tự mà bạn có thể vượt qua

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
0 dưới dạng giá trị cho tham số
q: Union[str, None] = Query(default=None)
6, bạn có thể vượt qua các giá trị khác.

Giả sử rằng bạn muốn khai báo tham số truy vấn from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(q: str | None = Query(default=None, max_length=50)): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) return results 6 để có from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(q: str | None = Query(default=None, max_length=50)): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) return results 9 là q: Union[str, None] = Query(default=None) 9 và có giá trị mặc định là q: Union[str, None] = None 0:

Ghi chú

Có một giá trị mặc định cũng làm cho tham số tùy chọn.

q: Union[str, None] = None

Làm cho nó yêu cầu và

payload = { 'topicIds':['128487' , '242793'],
8

Khi chúng ta không cần khai báo thêm xác nhận hoặc siêu dữ liệu, chúng ta có thể thực hiện tham số truy vấn

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
6 được yêu cầu chỉ bằng cách không khai báo giá trị mặc định, như:

payload = { 'topicIds':['128487' , '242793'],
9

thay vì:

Nhưng chúng tôi hiện đang tuyên bố nó với

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
7, ví dụ như:

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
0

Nhưng nó tuyên bố nó rõ ràng là một tham số truy vấn.

Thông tin

Hãy nhớ rằng phần quan trọng nhất để tạo một tham số tùy chọn là phần:

hoặc là:FastAPI know that this parameter is required.

Vì nó sẽ sử dụng from typing import Union from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(q: Union[str, None] = Query(default=None, max_length=50)): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) return results 0 làm giá trị mặc định và theo cách đó làm cho tham số không bắt buộc.

Phần

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
7 cho phép trình chỉnh sửa của bạn cung cấp hỗ trợ tốt hơn, nhưng đó không phải là điều cho Fastapi biết rằng tham số này không bắt buộc.

Sau đó, chúng ta có thể chuyển nhiều tham số hơn cho

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
7. Trong trường hợp này, tham số
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
1 áp dụng cho các chuỗi:

Bạn cũng có thể thêm một tham số

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
9:

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
1

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
2

Python 3.6 trở lên

Thêm biểu thức chính quy

Bạn có thể xác định một biểu thức thông thường mà tham số sẽ khớp:

Biểu thức chính quy cụ thể này kiểm tra xem giá trị tham số nhận được:

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
3

Python 3.6 trở lên

Thêm biểu thức chính quy

Bạn có thể xác định một biểu thức thông thường mà tham số sẽ khớp:

Biểu thức chính quy cụ thể này kiểm tra xem giá trị tham số nhận được:

q: Union[str, None] = Query(default=None)
0: Bắt đầu với các ký tự sau, không có ký tự trước đây.

q: Union[str, None] = Query(default=None)
1: có giá trị chính xác
q: Union[str, None] = Query(default=None)
1.

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
4

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
5

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
6

q: Union[str, None] = Query(default=None)
3: Kết thúc ở đó, không có thêm bất kỳ ký tự nào sau
q: Union[str, None] = Query(default=None)
1.

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
7

Nếu bạn cảm thấy lạc lõng với tất cả những ý tưởng "biểu hiện chính quy" này, đừng lo lắng. Họ là một chủ đề khó khăn cho nhiều người. Bạn vẫn có thể làm rất nhiều thứ mà không cần biểu cảm thường xuyên.

Vì vậy, phản hồi cho url đó sẽ là:

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
8

Mẹo

Để khai báo một tham số truy vấn với một loại

payload = { 'topicIds':['128487' , '242793'],
17, như trong ví dụ trên, bạn cần sử dụng rõ ràng
from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
7, nếu không nó sẽ được hiểu là một cơ quan yêu cầu.

Các tài liệu API tương tác sẽ cập nhật phù hợp, để cho phép nhiều giá trị:

Hướng dẫn python requests query params multiple values - python yêu cầu truy vấn tham số nhiều giá trị

Danh sách tham số truy vấn / nhiều giá trị với mặc định

Và bạn cũng có thể xác định một mặc định

payload = { 'topicIds':['128487' , '242793'],
17 của các giá trị nếu không có gì được cung cấp:

Python 3.6 trở lên

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
9

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
0

Nếu bạn đi đến:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
1

Mặc định của

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
6 sẽ là:
payload = { 'topicIds':['128487' , '242793'],
23 và phản hồi của bạn sẽ là:

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
8

Sử dụng ________ 117¶

Bạn cũng có thể sử dụng

payload = { 'topicIds':['128487' , '242793'],
17 trực tiếp thay vì
payload = { 'topicIds':['128487' , '242793'],
26 (hoặc
payload = { 'topicIds':['128487' , '242793'],
27 trong Python 3.9+):

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
3

Ghi chú

Hãy nhớ rằng trong trường hợp này, Fastapi sẽ không kiểm tra nội dung của danh sách.

Ví dụ:

payload = { 'topicIds':['128487' , '242793'],
28 sẽ kiểm tra (và tài liệu) rằng nội dung của danh sách là số nguyên. Nhưng
payload = { 'topicIds':['128487' , '242793'],
17 một mình sẽ không.

Bạn có thể thêm thông tin về tham số.

Thông tin đó sẽ được bao gồm trong OpenAPI được tạo và được sử dụng bởi các giao diện người dùng tài liệu và các công cụ bên ngoài.

Ghi chú

Hãy nhớ rằng trong trường hợp này, Fastapi sẽ không kiểm tra nội dung của danh sách.

Ví dụ:

payload = { 'topicIds':['128487' , '242793'],
28 sẽ kiểm tra (và tài liệu) rằng nội dung của danh sách là số nguyên. Nhưng
payload = { 'topicIds':['128487' , '242793'],
17 một mình sẽ không.

Bạn có thể thêm thông tin về tham số.

Thông tin đó sẽ được bao gồm trong OpenAPI được tạo và được sử dụng bởi các giao diện người dùng tài liệu và các công cụ bên ngoài.

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
4

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
5

Hãy nhớ rằng các công cụ khác nhau có thể có các mức hỗ trợ OpenAPI khác nhau.

Thông tin đó sẽ được bao gồm trong OpenAPI được tạo và được sử dụng bởi các giao diện người dùng tài liệu và các công cụ bên ngoài.

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
6

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
7

Hãy nhớ rằng các công cụ khác nhau có thể có các mức hỗ trợ OpenAPI khác nhau.

Một số trong số họ có thể không hiển thị tất cả các thông tin bổ sung được tuyên bố, mặc dù trong hầu hết các trường hợp, tính năng còn thiếu đã được lên kế hoạch để phát triển.

Bạn có thể thêm

payload = { 'topicIds':['128487' , '242793'],
30:

Python 3.6 trở lên

Và A

payload = { 'topicIds':['128487' , '242793'],
31:

Thông số bí danh

Hãy tưởng tượng rằng bạn muốn tham số là

payload = { 'topicIds':['128487' , '242793'],
32.

Giống như trong:

Thông tin đó sẽ được bao gồm trong OpenAPI được tạo và được sử dụng bởi các giao diện người dùng tài liệu và các công cụ bên ngoài.

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
9

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
0

Hãy nhớ rằng các công cụ khác nhau có thể có các mức hỗ trợ OpenAPI khác nhau.

Một số trong số họ có thể không hiển thị tất cả các thông tin bổ sung được tuyên bố, mặc dù trong hầu hết các trường hợp, tính năng còn thiếu đã được lên kế hoạch để phát triển.

Bạn có thể thêm

payload = { 'topicIds':['128487' , '242793'],
30:

Python 3.6 trở lên

Thông tin đó sẽ được bao gồm trong OpenAPI được tạo và được sử dụng bởi các giao diện người dùng tài liệu và các công cụ bên ngoài.

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
1

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
2

Hãy nhớ rằng các công cụ khác nhau có thể có các mức hỗ trợ OpenAPI khác nhau.

Hướng dẫn python requests query params multiple values - python yêu cầu truy vấn tham số nhiều giá trị

Một số trong số họ có thể không hiển thị tất cả các thông tin bổ sung được tuyên bố, mặc dù trong hầu hết các trường hợp, tính năng còn thiếu đã được lên kế hoạch để phát triển.

Bạn có thể thêm

payload = { 'topicIds':['128487' , '242793'],
30:

Thông tin đó sẽ được bao gồm trong OpenAPI được tạo và được sử dụng bởi các giao diện người dùng tài liệu và các công cụ bên ngoài.

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
3

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
4

Hãy nhớ rằng các công cụ khác nhau có thể có các mức hỗ trợ OpenAPI khác nhau.

Một số trong số họ có thể không hiển thị tất cả các thông tin bổ sung được tuyên bố, mặc dù trong hầu hết các trường hợp, tính năng còn thiếu đã được lên kế hoạch để phát triển.

Bạn có thể thêm

payload = { 'topicIds':['128487' , '242793'],
30:

  • payload = { 'topicIds':['128487' , '242793'],
    
    36
  • payload = { 'topicIds':['128487' , '242793'],
    
    30
  • payload = { 'topicIds':['128487' , '242793'],
    
    31
  • payload = { 'topicIds':['128487' , '242793'],
    
    45

Python 3.6 trở lên

  • from fastapi import FastAPI, Query
    
    app = FastAPI()
    
    
    @app.get("/items/")
    async def read_items(q: str | None = Query(default=None, max_length=50)):
        results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
        if q:
            results.update({"q": q})
        return results
    
    9
  • from fastapi import FastAPI, Query
    
    app = FastAPI()
    
    
    @app.get("/items/")
    async def read_items(q: str | None = Query(default=None, max_length=50)):
        results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
        if q:
            results.update({"q": q})
        return results
    
    1
  • payload = { 'topicIds':['128487' , '242793'],
    
    48

Và A

payload = { 'topicIds':['128487' , '242793'],
31:

Thông số bí danh

Làm cách nào để vượt qua nhiều tham số trong yêu cầu nhận?

Bạn có nhiều tùy chọn để chuyển nhiều tham số cho phương thức GET: FromRouteAttribution, FromQuery và Model Binding ...
Các trường biểu mẫu ..
Yêu cầu cơ thể ..
Thông số dữ liệu định tuyến ..
Tham số chuỗi truy vấn ..
Đã tải lên các tệp ..

Làm thế nào để bạn vượt qua nhiều giá trị trong một tham số Postman?

Nhiều tham số trong URL trên, '&' phải được theo sau bởi một tham số như & IE = UTF-8.Trong tham số này, tức là, là khóa và, UTF-8 là giá trị khóa.Nhập cùng một URL trong trường Văn bản Postman;Bạn sẽ nhận được nhiều tham số trong tab params.In the above URL, '&' should be followed by a parameter such as &ie=UTF-8. In this parameter, i.e., is the key and, UTF-8 is the key-value. Enter the same URL in the Postman text field; you will get the multiple parameters in the Params tab.

Làm cách nào để chuyển nhiều tham số trong URL API?

Vượt qua nhiều tham số trong URL trong API Web..
Trong "Giải pháp thám hiểm" ..
Mở rộng thư mục "lượt xem" ..
Chọn "Trang chủ" -> "INDEX. CSHTML".Thêm mã sau: @{Viewbag.Title = "index";} Truyền nhiều tham số trong url @Using (html. BeginForm ("index", "home")) {

Làm thế nào để bạn thêm nhiều tham số truy vấn vào URL?

Trong một URL, truy vấn bắt đầu với một dấu hỏi, với nhiều tham số truy vấn được phân tách bằng ampersands ("&") ...
Ví dụ: URL này có 2 tham số: https://example.com/page?....
Gợi ý sẽ kích hoạt cho bất kỳ URL nào có hai hoặc nhiều dấu hỏi, ví dụ:.
http://example.com/page?.