question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'after_request' runs but headers don't get set

See original GitHub issue
import bottle


app = bottle.Bottle()

@app.hook('after_request')
def fn():
    print(dict(bottle.response.headers))
    bottle.response.headers['abc'] = 'yes'
    print(dict(bottle.response.headers))


@app.get('/')
def asdf():
    return ''


app.run()

This code runs properly. I’m able to get the headers when I make a request from another program. However if I change the route to

@app.get('/')
def asdf():
    bottle.abort(424)

The hook gets run since I can see it in the server process log

Bottle v0.12.13 server starting up (using WSGIRefServer())...
Listening on http://127.0.0.1:8080/
Hit Ctrl-C to quit.

{}
{'abc': 'yes'}
127.0.0.1 - - [15/Jan/2019 21:23:03] "GET / HTTP/1.1" 424 726

but the header is not received in the program which made the call. That program is also quiet simple. curl http://localhost:8080 -D-

I saw #978 and was facing the same problem. Some of the UI frameworks using our APIs break if the error response does not have CORS headers.

Am I doing something wrong?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
defnullcommented, Jan 31, 2019

It’s solved in 0.13 but this gets reported quite often for 0.12, so I’ll look into a backport or pull requests fixing this specific issue for 0.12. Let’s keep this issue open for now.

2reactions
nicoabiecommented, Jan 25, 2019

This issue is still valid in latest stable release which is 0.12.16 at the moment of writting this comment.

Particularly fails when working with static_files:

@hook('after_request')
def enable_cors_after_request_hook():
   print("add_cors_headers")
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
    response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'

@get('/benchmark/<feed>/<strategy>/<name>')
def bench_get(feed, strategy, name):
    return static_file(name + '.json', root="../benchs/{}/{}".format(feed, strategy))

It calls the function but header are not set.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Go - After request headers are set, request body is empty
Background; NewRequestWithContext set getBody(). You should have that function already defined for your http.NewRequest request.
Read more >
axios doesn't see request headers that I set #891 - GitHub
Hi, I tried to make a CORS API post call using axios but I've been never able to do that because I must...
Read more >
HTTP | Node.js v19.3.0 Documentation
Sets a single header value for headers object. If this header already exists in the to-be-sent headers, its value will be replaced. Use...
Read more >
Using Axios to set request headers - LogRocket Blog
Learn different ways Axios can be used to set request headers for API ... visibility to ensure your users don't run into unknown...
Read more >
API — Flask Documentation (2.2.x)
If the import name is not properly set up, that debugging information is lost. ... view_func does not necessarily need to be passed,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found