Home > Blockchain >  Flask headers not sending
Flask headers not sending

Time:05-05

I have an application where I am trying to send a post request with an added header.

resp=make_response(redirect(returnFilter(request.args,forms)))
resp.headers.add('foo','foo')
return resp

the returnFilter function is a custom function to make a link for the redirect and that works fine. I added a breakpoint at return resp to make sure it was adding the header after I noticed the problem and it does.

debug picture

I caught the request it would send and it confirmed it did not actually add the header but I do not know why.

caught picture

CodePudding user response:

You are adding the foo: foo header to the response headers, but it looks like your screenshot is from request headers, did you look at the correct headers?

Some example code i wrote to test headers.add(...) method:

from flask import Flask, redirect, url_for, make_response

app = Flask(__name__)

@app.get('/')
def index():
    return 'Hello'

@app.get('/header-test')
def header_test():
    response = make_response(redirect(url_for('index')))
    response.headers.add('foo', 'foo')
    return response

app.run()

Request headers:

GET /header-test HTTP/1.1
Host: 127.0.0.1:5000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0
Accept: text/html,application/xhtml xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: fi-FI,fi;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1

Response headers:

HTTP/1.1 302 FOUND
Server: Werkzeug/2.1.1 Python/3.10.4
Date: Mon, 02 May 2022 00:53:07 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 208
Location: /
foo: foo

As you can see foo: foo is there in response headers just like it should be.

  • Related