QLands
QLands

Reputation: 2586

Add http header in HTTPFound() with pyramid

I have a Pyramid application where I have the following line of code:

return HTTPFound(location=request.route_url('feeds'))

However I want to pass an extra parameter in the headers. Im trying with this:

headers = {"MyVariable": "MyValue"}
return HTTPFound(location=request.route_url('feeds'),headers=headers)

However the view_config of "feeds" does not get MyVariable in the headers. I'm checking it with the following code:

print "**************"
for key in request.headers.keys():
   print key
print "**************"

What am I doing wrong?

Upvotes: 0

Views: 1753

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1122042

headers is meant to be a sequence of (key, value) pairs:

headers = [("MyVariable", "MyValue")]

This lets you specify a header more than once. Also see the Response documentation, the headers keyword is passed on as headerlist to the Response object produced. Also see the HTTP Exceptions documentation:

headers: a list of (k,v) header pairs

However, headers are only sent to the client; they are not passed on by the client to the next request that they are instructed to make. Use GET query parameters if you need to pass information along to the redirection target, or set values in cookies or in the session instead.

To add on query parameters, specify a _query directory for route_url():

params = {"MyVariable": "MyValue"}
return HTTPFound(location=request.route_url('feeds', _query=params))

and look for those query parameters in request.GET:

for key in request.GET:
    print key, request.GET.getall(key)

Upvotes: 1

X-Istence
X-Istence

Reputation: 16667

Due to the way HTTP works, what you are asking is not possible. You can use either GET parameters to pass the data, or you can store the data in a cookie instead.

Upvotes: 0

Related Questions