from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.exceptions import HTTPException
from json.decoder import JSONDecodeError
import uvicorn
app = Starlette(debug=True)
@app.exception_handler(JSONDecodeError)
async def log_exceptions(request, exc):
body: bytes = await request.body() # hangs forever
body_text: str = bytes.decode(body)
assert body_text, "I could not retrieve the body"
return JSONResponse({ "error": "Unexpected error happened!" })
@app.route('/api', methods=["POST"])
async def homepage(request):
body_bytes = await request.body()
if body_bytes:
json = await request.json()
print(json)
print("-----")
print(request.headers)
return JSONResponse(json)
return JSONResponse({ "error": "empty body, please send something" })
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=7000)
'Development > Python' 카테고리의 다른 글
[selenium] 크롤링 (0) | 2022.12.19 |
---|---|
[Python] pip3 install paho-mqtt==1.6.1 (0) | 2022.08.25 |
[python] requirements.txt로 패키지 관리 (0) | 2022.03.10 |
[Python]csv 엑셀 변환 (0) | 2020.11.19 |
[Python]잡동사니 (0) | 2020.09.25 |