question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] How to specify json data input in docs for request?

See original GitHub issue

Question

How can I make my Input parameters show up in the swagger docs for a JSON request? I make requests like this …

curl 'http://127.0.0.1:5000/predict' -H "Content-Type: application/json" --data '{"data": ["string1", "string2"]}'

from fastapi import FastAPI
from starlette.requests import Request
from pydantic import BaseModel
from typing import List

app = FastAPI()

class Prediction(BaseModel):
    term: str
    label: str
    score: str

class Output(BaseModel):
    success: bool
    prediction: List[Prediction] = []

# .. this is probably wrong.
class Input(Request, BaseModel): 
    data: List[str] = []

@app.post('/predict', response_model=Output)
async def predict(request: Input):

    data = Output(success=False)

    try:
        params = await request.json()
    except Exception as e:
        print(e)
    ...
    data.success = True
    return data

Swagger docs without input parameters

Screen Shot 2020-02-28 at 9 49 08 AM

I would like this to show the input schema so the user knows they’re supposed to POST with {"data": [list of strings])

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Toad2186commented, Mar 4, 2020

@Shane-Neeley You don’t need to await request.json(). You can take @iishyfishyy’s advice and remove the Request inheritance:

class Input(BaseModel):
    data: List[str] = []
...
async def predict(request: Input):
    ...
    # Prints ['string1', 'string2'] in your example
    print(request.data)
    ...

fastapi will take care of deserializing the body into request for you, so there’s no need to await request.json(). Also note that it will show up as a “Request body.” “Parameters” is for url (query) params. Screen Shot 2020-03-03 at 8 23 09 PM

1reaction
iishyfishyycommented, Feb 28, 2020

Shouldn’t this

class Input(Request, BaseModel): 
    data: List[str] = []

be this

class Input(BaseModel): 
    data: List[str] = []

I don’t think you need Request in your input model.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with JSON data in Google Standard SQL | BigQuery
Given a JSON type in BigQuery, you can access the fields in a JSON expression by using the field access operator. The following...
Read more >
sending file and json in POST multipart/form-data request with ...
I managed to make a correct request via Postman, by sending document as a .json file. Though I discovered this only works on...
Read more >
Working with JSON - Learn web development | MDN
In this article, we've given you a simple guide to using JSON in your programs, including how to create and parse JSON, and...
Read more >
Getting Started Step-By-Step - JSON Schema
To start a schema definition, let's begin with a basic JSON schema. We start with four properties called keywords which are expressed as...
Read more >
Using REST to Invoke the API | Programmable Search Engine
This document describes how to use the Custom Search JSON API. ... Search query - Use the q query parameter to specify your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found