3.2 Handling GET Requests
In previous section, we created a simple HTTP trigger that returns a fixed string. In this section, we will create a new HTTP trigger that returns a string based on the query parameters of the request.
Say Hello To Someone
Before we start writing the code, we need to install the pydantic
package in your
project awel-tutorial
poetry add "pydantic>=2.6.0"
Then create a new file named http_trigger_say_hello.py
in the awel_tutorial
directory and add the following code:
from dbgpt._private.pydantic import BaseModel, Field
from dbgpt.core.awel import DAG, HttpTrigger, MapOperator, setup_dev_environment
class TriggerReqBody(BaseModel):
name: str = Field(..., description="User name")
age: int = Field(18, description="User age")
with DAG("awel_say_hello") as dag:
trigger_task = HttpTrigger(
endpoint="/awel_tutorial/say_hello",
methods="GET",
request_body=TriggerReqBody,
status_code=200
)
task = MapOperator(
map_function=lambda x: f"Hello, {x.name}! You are {x.age} years old."
)
trigger_task >> task
setup_dev_environment([dag], port=5555)
And run the following command to execute the code:
poetry run python awel_tutorial/http_trigger_say_hello.py
Now, open a new terminal and run the following command to send a GET request to the server:
curl -X GET \
"http://127.0.0.1:5555/api/v1/awel/trigger/awel_tutorial/say_hello?name=John&age=25"