could you tell me why it doesn't work when passed as JSON?
When you register a route with
@app.post('/ints')
def post_int(x: int, y: int):
return x, y
in FastApi, the endpoint is added in
decorator (routing.py)
add_api_route (routing.py)
__init__ (routing.py)
get_dependant (dependencies/utils.py)
analyze_param (dependencies/utils.py)
Near the end of analyze_param
, you can find
if is_path_param:
# We might check here that `default_value is RequiredParam`, but the fact is that the same
# parameter might sometimes be a path parameter and sometimes not. See
# `tests/test_infer_param_optionality.py` for an example.
field_info = params.Path(annotation=use_annotation)
elif is_uploadfile_or_nonable_uploadfile_annotation(
type_annotation
) or is_uploadfile_sequence_annotation(type_annotation):
field_info = params.File(annotation=use_annotation, default=default_value)
elif not field_annotation_is_scalar(annotation=type_annotation):
field_info = params.Body(annotation=use_annotation, default=default_value)
else:
field_info = params.Query(annotation=use_annotation, default=default_value)
https://github.com/fastapi/fastapi/blob/master/fastapi/dependencies/utils.py#L460
There you can see that scalar parameters have field info type query. Lists aren't scalar values and have field info type body.