We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Staging is currently returning HTML to bodhi CLI requests
I am not sure why this is happening, but it seems that staging Bodhi is currently returning HTML to CLI requests. This also happens to requests with ```http``` or ```curl```.

I recall a problem with the unit tests where they would sometimes receive HTML when they didn't explicitly use a request header to ask for a JSON response once we started testing under Python 3. We ended up adjusting the tests to pass that header since this did not seem to happen when serving Bodhi with ```pserve-3```.

It turns out that there really is some problem that seems related to Python 3 since staging Bodhi started doing this same thing.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `bodhi/server/webapp.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 # Copyright © 2018 Red Hat, Inc.
3 #
4 # This file is part of Bodhi.
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 """
20 Define Bodhi's WSGI application.
21 
22 As of the writing of this docblock, this module is a bit misnamed since the webapp is actually
23 defined in bodhi.server.__init__. However, that is an anti-pattern with lots of nasty in-line
24 imports due to circular dependencies, and this module is intended to solve that problem.
25 Unfortunately, it is a backwards-incompatible change to move main() here, so it will remain in
26 __init__ until we make a major Bodhi release. See https://github.com/fedora-infra/bodhi/issues/2294
27 """
28 
29 from pyramid.events import NewRequest, subscriber
30 
31 from bodhi import server
32 
33 
34 def _complete_database_session(request):
35     """
36     Commit the database changes if no exceptions occurred.
37 
38     This is a post-request hook. It handles rolling back or committing the session based on whether
39     an exception occurred or not. To get a database session that's not tied to the request/response
40     cycle, just use the :data:`Session` scoped session.
41 
42     Args:
43         request (pyramid.request.Request): The current web request.
44     """
45     _rollback_or_commit(request)
46     server.Session().close()
47     server.Session.remove()
48 
49 
50 @subscriber(NewRequest)
51 def _prepare_request(event):
52     """
53     Add callbacks onto every new request.
54 
55     This function adds a callback to clean up the database session when the request is finished.
56 
57     Args:
58         event (pyramid.events.NewRequest): The new request event.
59     """
60     event.request.add_finished_callback(_complete_database_session)
61 
62 
63 def _rollback_or_commit(request):
64     """
65     Commit the transaction if there are no exceptions, otherwise rollback.
66 
67     Args:
68         request (pyramid.request.Request): The current web request.
69     """
70     if request.exception is not None:
71         server.Session().rollback()
72     else:
73         server.Session().commit()
74
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/bodhi/server/webapp.py b/bodhi/server/webapp.py
--- a/bodhi/server/webapp.py
+++ b/bodhi/server/webapp.py
@@ -50,13 +50,25 @@
 @subscriber(NewRequest)
 def _prepare_request(event):
     """
-    Add callbacks onto every new request.
+    Prepare each incoming request to Bodhi.
 
-    This function adds a callback to clean up the database session when the request is finished.
+    This function does two things:
+        * If requests do not have an Accept header, or if their Accept header is "*/*", it sets the
+          header to application/json. Pyramid has undefined behavior when an ambiguous or missing
+          Accept header is received, and multiple views are defined that handle specific Accept
+          headers. For example, we have a view that returns html or JSON for /composes/, depending
+          on the Accept header, but if a request has no Accept header or has */*, Pyramid will
+          consider both views to be a match for the request and so it is undefined which view will
+          handle the request. Let's force ambibuous requests to receive a JSON response so we have a
+          defined behavior. See https://github.com/fedora-infra/bodhi/issues/2731.
+        * It adds a callback to clean up the database session when the request is finished.
 
     Args:
         event (pyramid.events.NewRequest): The new request event.
     """
+    if 'Accept' not in event.request.headers or event.request.headers['Accept'] == '*/*':
+        event.request.headers['Accept'] = 'application/json'
+
     event.request.add_finished_callback(_complete_database_session)

{"golden_diff": "diff --git a/bodhi/server/webapp.py b/bodhi/server/webapp.py\n--- a/bodhi/server/webapp.py\n+++ b/bodhi/server/webapp.py\n@@ -50,13 +50,25 @@\n @subscriber(NewRequest)\n def _prepare_request(event):\n     \"\"\"\n-    Add callbacks onto every new request.\n+    Prepare each incoming request to Bodhi.\n \n-    This function adds a callback to clean up the database session when the request is finished.\n+    This function does two things:\n+        * If requests do not have an Accept header, or if their Accept header is \"*/*\", it sets the\n+          header to application/json. Pyramid has undefined behavior when an ambiguous or missing\n+          Accept header is received, and multiple views are defined that handle specific Accept\n+          headers. For example, we have a view that returns html or JSON for /composes/, depending\n+          on the Accept header, but if a request has no Accept header or has */*, Pyramid will\n+          consider both views to be a match for the request and so it is undefined which view will\n+          handle the request. Let's force ambibuous requests to receive a JSON response so we have a\n+          defined behavior. See https://github.com/fedora-infra/bodhi/issues/2731.\n+        * It adds a callback to clean up the database session when the request is finished.\n \n     Args:\n         event (pyramid.events.NewRequest): The new request event.\n     \"\"\"\n+    if 'Accept' not in event.request.headers or event.request.headers['Accept'] == '*/*':\n+        event.request.headers['Accept'] = 'application/json'\n+\n     event.request.add_finished_callback(_complete_database_session)\n", "issue": "Staging is currently returning HTML to bodhi CLI requests\nI am not sure why this is happening, but it seems that staging Bodhi is currently returning HTML to CLI requests. This also happens to requests with ```http``` or ```curl```.\r\n\r\nI recall a problem with the unit tests where they would sometimes receive HTML when they didn't explicitly use a request header to ask for a JSON response once we started testing under Python 3. We ended up adjusting the tests to pass that header since this did not seem to happen when serving Bodhi with ```pserve-3```.\r\n\r\nIt turns out that there really is some problem that seems related to Python 3 since staging Bodhi started doing this same thing.\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n# Copyright \u00a9 2018 Red Hat, Inc.\n#\n# This file is part of Bodhi.\n#\n# This program is free software; you can redistribute it and/or\n# modify it under the terms of the GNU General Public License\n# as published by the Free Software Foundation; either version 2\n# of the License, or (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with this program; if not, write to the Free Software\n# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n\"\"\"\nDefine Bodhi's WSGI application.\n\nAs of the writing of this docblock, this module is a bit misnamed since the webapp is actually\ndefined in bodhi.server.__init__. However, that is an anti-pattern with lots of nasty in-line\nimports due to circular dependencies, and this module is intended to solve that problem.\nUnfortunately, it is a backwards-incompatible change to move main() here, so it will remain in\n__init__ until we make a major Bodhi release. See https://github.com/fedora-infra/bodhi/issues/2294\n\"\"\"\n\nfrom pyramid.events import NewRequest, subscriber\n\nfrom bodhi import server\n\n\ndef _complete_database_session(request):\n    \"\"\"\n    Commit the database changes if no exceptions occurred.\n\n    This is a post-request hook. It handles rolling back or committing the session based on whether\n    an exception occurred or not. To get a database session that's not tied to the request/response\n    cycle, just use the :data:`Session` scoped session.\n\n    Args:\n        request (pyramid.request.Request): The current web request.\n    \"\"\"\n    _rollback_or_commit(request)\n    server.Session().close()\n    server.Session.remove()\n\n\n@subscriber(NewRequest)\ndef _prepare_request(event):\n    \"\"\"\n    Add callbacks onto every new request.\n\n    This function adds a callback to clean up the database session when the request is finished.\n\n    Args:\n        event (pyramid.events.NewRequest): The new request event.\n    \"\"\"\n    event.request.add_finished_callback(_complete_database_session)\n\n\ndef _rollback_or_commit(request):\n    \"\"\"\n    Commit the transaction if there are no exceptions, otherwise rollback.\n\n    Args:\n        request (pyramid.request.Request): The current web request.\n    \"\"\"\n    if request.exception is not None:\n        server.Session().rollback()\n    else:\n        server.Session().commit()\n", "path": "bodhi/server/webapp.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n# Copyright \u00a9 2018 Red Hat, Inc.\n#\n# This file is part of Bodhi.\n#\n# This program is free software; you can redistribute it and/or\n# modify it under the terms of the GNU General Public License\n# as published by the Free Software Foundation; either version 2\n# of the License, or (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with this program; if not, write to the Free Software\n# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n\"\"\"\nDefine Bodhi's WSGI application.\n\nAs of the writing of this docblock, this module is a bit misnamed since the webapp is actually\ndefined in bodhi.server.__init__. However, that is an anti-pattern with lots of nasty in-line\nimports due to circular dependencies, and this module is intended to solve that problem.\nUnfortunately, it is a backwards-incompatible change to move main() here, so it will remain in\n__init__ until we make a major Bodhi release. See https://github.com/fedora-infra/bodhi/issues/2294\n\"\"\"\n\nfrom pyramid.events import NewRequest, subscriber\n\nfrom bodhi import server\n\n\ndef _complete_database_session(request):\n    \"\"\"\n    Commit the database changes if no exceptions occurred.\n\n    This is a post-request hook. It handles rolling back or committing the session based on whether\n    an exception occurred or not. To get a database session that's not tied to the request/response\n    cycle, just use the :data:`Session` scoped session.\n\n    Args:\n        request (pyramid.request.Request): The current web request.\n    \"\"\"\n    _rollback_or_commit(request)\n    server.Session().close()\n    server.Session.remove()\n\n\n@subscriber(NewRequest)\ndef _prepare_request(event):\n    \"\"\"\n    Prepare each incoming request to Bodhi.\n\n    This function does two things:\n        * If requests do not have an Accept header, or if their Accept header is \"*/*\", it sets the\n          header to application/json. Pyramid has undefined behavior when an ambiguous or missing\n          Accept header is received, and multiple views are defined that handle specific Accept\n          headers. For example, we have a view that returns html or JSON for /composes/, depending\n          on the Accept header, but if a request has no Accept header or has */*, Pyramid will\n          consider both views to be a match for the request and so it is undefined which view will\n          handle the request. Let's force ambibuous requests to receive a JSON response so we have a\n          defined behavior. See https://github.com/fedora-infra/bodhi/issues/2731.\n        * It adds a callback to clean up the database session when the request is finished.\n\n    Args:\n        event (pyramid.events.NewRequest): The new request event.\n    \"\"\"\n    if 'Accept' not in event.request.headers or event.request.headers['Accept'] == '*/*':\n        event.request.headers['Accept'] = 'application/json'\n\n    event.request.add_finished_callback(_complete_database_session)\n\n\ndef _rollback_or_commit(request):\n    \"\"\"\n    Commit the transaction if there are no exceptions, otherwise rollback.\n\n    Args:\n        request (pyramid.request.Request): The current web request.\n    \"\"\"\n    if request.exception is not None:\n        server.Session().rollback()\n    else:\n        server.Session().commit()\n", "path": "bodhi/server/webapp.py"}]}