1
votes

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
Usually the backslash is because of it being escaped in the output format; when you read the response in your client it will be converted to its original form. If you return a plain text response it should not be visible. fastapi.tiangolo.com/advanced/custom-response/… - MatsLindh

1 Answers

4
votes

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"'