This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How ot handle generic text.plain POST requests #579
Comments
I think this has been asked several times already, maybe it was on the chat though 😉
|
@euri10 thanks for the hint but i get teh following error:
Any further advice? Any documentation I can read to better understand the Body class? |
@euri10 actually, You can use EDIT: Removed my original response showing the use of |
I'm not sure why you ran into an error with trying to read the data off the body. The following code works for me: from fastapi import FastAPI
from starlette.requests import Request
from starlette.responses import Response
from starlette.testclient import TestClient
app = FastAPI()
@app.post("/webhook", response_class=Response)
async def the_webhook(request: Request):
return await request.body()
data = b"""EURUSD Less Than 1.09092
{"Condition": "value"}
[3,4,5,]
{}"""
client = TestClient(app)
response = client.post("/webhook", data=data)
print(response.content)
# b'EURUSD Less Than 1.09092\n{"Condition": "value"}\n[3,4,5,]\n{}' Without the Maybe you can share your error message if this isn't working for you? |
@dmontagu Thank you so much for refocusing me and confirming the approach. This does work for me . - I have no idea what went wrong previously. For clarity could you please explain or direct me to the documentation about what the |
tl;dr:
The pydantic The problem with this is that (Note, there are other ways in python to achieve this goal that might not involve the use of Since You can also make the field optional by with a non-None default: -- for example, if you had the endpoint function @app.post("/")
def f(x: Dict[str, Any] = Body({}), y: Dict[str, Any] = Body({})):
... Then the variable This use of |
This is also described in various places in the docs, though not quite as head-on as the description above. For example, this is explained in the section titled "Use |
@dmontagu Thank you so much for the clarification and detailed explanation. I will continue my missing of advocating FastAPI ! Regards! |
Hello @CockyAmoeba and @dmontagu , I am new to FastAPI. Do you know how text.plain in POST request behaves in OpenAPI doc? When I follow the example you created, Thanks! |
@phy25 request: Dict[str, Any] = Body('', media_type="text/plain"),
request: Any = Body(..., media_type="text/turtle"), These seem to add it to OpenAPI documentation, but now the proper request is rejected and saying:
What's the right schema for |
I believe the problem here is that when you use If you just want the text of the body, try using It should be possible to override the openapi spec generation to ensure this gets documented properly. @tiangolo may have some better idea. |
@dmontagu Thanks for the information. I was able to use Request as the input type, and, as you said, Request seems overriding the openapi spec.
|
@tiangolo I could update Line 114 in 9c3c9b6
like this: if body_bytes and request.headers['Content-Type'] == 'application/json':
# if body_bytes and request.headers.get('Content-Type', 'application/json'): # if the content type should be assumed.
body = await request.json()
else:
body = body_bytes This works for me but not sure if there would be any other implications. Let me know if you'd like me to PR this one. |
@jbkoh I think this is worth a separate issue. I'm not sure if your approach is the best/safest way to handle this (given who knows what people might be doing now with the content-type header), but even if not it might still be worth providing some easy way to get the OpenAPI media type documented properly. Actually, it wouldn't surprise me if this was already possible in some way that was a little closer to your |
@dmontagu I will create a new Issue and continue this discussion there. Thanks for the guidance. |
Thanks for the help here everyone! 👏 🙇 Thanks for reporting back and closing the issue @CockyAmoeba 👍 |
Hi @jbkoh . Any update on this or the other issue that you opened? |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Description
I am trying to write an endpoint to accept a request that is only sending text/plain body. I am struggling ot understand from the documentation how to handle this and have been getting a raft of errors
Are you able to provide sage advice? Is this possible a due to FASTAPI being "typed" and expecting json data to fulfil pydantic model schema?
header of request begin made to my api:
example payload:
Additional context
I first tried:
But that would give - [2019-09-30 12:19:45 +0000] [8] [INFO] ('172.17.0.1', 55782) - "POST /webhook HTTP/1.1" 422
Which was also unsuccessfull.
I REALLY wish to use FastAPI going forward but i am struggling ot understand how to do VERY basic things as the framework is trying to enforce schema and structure - I know this is the ideal but in this scenario I have no control on the incoming webhook that I need to catch!
I hope someone can help!
Regards,
The text was updated successfully, but these errors were encountered: