Facing issue while returning content type image/svg+xml?

Viewed 149

I have built a flask project where I am returning a SVG image from one of the endpoints. I am having trouble as the output rendered is simple plain-text. Following is the SVG code.

@app.route("/test/<string:name>")
def testApi(name: str):
    svg = """
        <svg width='450' height='190' viewBox="0 0 100 100" xxmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
            
        <rect x="0" y="0" width="120" height="100" fill="#FFFFFF" stroke='blue' stroke-width="5"></rect>
        <g>
            <circle id="sd" class="medium" cx="85" cy="55" r="13" fill="url(#image)" stroke='red' stroke-width="3" />
            <text x="10" y="20" font-size="10" font-family="Verdana">
                    <tspan x='7' y='20'>Name </tspan><tspan x='40' y='20' font-weight="bold">Praveen</tspan>
                    <tspan x='7' y='35'>Email </tspan><tspan x='40' y='35' font-weight="bold" style='fill:red'>pruvi007</tspan>
                </text>         
            </g>
            
                
        </svg>
        """
    return flask.Response(svg, mimetype="image/svg+xml")

If I return just svg as it is (means not specifying the content type), I receive the desired output. However, changing it to image/svg+xml , I get plain text.

I have attached the screenshots of the output as well. With return type mentioning the content-type, I get this

Not Desired:

IMAGE1-withContentType

Desired:

IMAGE2-withoutContentType

So, How to get the Desired output by including the Content Type in return response?

0 Answers
Related