How ot handle generic text.plain POST requests #8108
Replies: 18 comments
-
I think this has been asked several times already, maybe it was on the chat though 😉
|
Beta Was this translation helpful? Give feedback.
-
@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? |
Beta Was this translation helpful? Give feedback.
-
@euri10 actually, You can use EDIT: Removed my original response showing the use of |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
@dmontagu Thank you so much for the clarification and detailed explanation. I will continue my missing of advocating FastAPI ! Regards! |
Beta Was this translation helpful? Give feedback.
-
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! |
Beta Was this translation helpful? Give feedback.
-
@jbkoh Reference: #439 |
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
@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.
|
Beta Was this translation helpful? Give feedback.
-
@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. |
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
@dmontagu I will create a new Issue and continue this discussion there. Thanks for the guidance. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the help here everyone! 👏 🙇 Thanks for reporting back and closing the issue @CockyAmoeba 👍 |
Beta Was this translation helpful? Give feedback.
-
Hi @jbkoh . Any update on this or the other issue that you opened? |
Beta Was this translation helpful? Give feedback.
-
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,
Beta Was this translation helpful? Give feedback.
All reactions