Fastapi response - backslash

Viewed 1286

I have variable holds time format of sql, and need to return it,but it being esacped with backslash while i print it back it doesn't

see example:


@app.get("/")

def test_string():
    sql_date ='YYYY-MM-DD"HH24:MI:SS"Z"'
    print(sql_date)
#get YYYY-MM-DD"HH24:MI:SS"Z"
    return sql_date
#return "YYYY-MM-DD\"HH24:MI:SS\"Z\""

How can i get the return from fastapi without the escaping backslash?

1 Answers

Use a different response class instead of JSONResponse. For your case, the raw Response class may be enough.

For example:

from fastapi import Response
@app.get("/")
def test_string():
    return Response(content = 'YYYY-MM-DD"HH24:MI:SS"Z"')

Or

from fastapi import Response
@app.get("/", response_class = Response)
def test_string():
    return 'YYYY-MM-DD"HH24:MI:SS"Z"'
Related