mod_wsgi: TypeError: expected byte string object for header name, value of type unicode found
I have a flask web app running in Production using Python 2.7.5 and mod_wsgi 3.4. The webapp runs fine in production and local loopback.
I recently added a Flask Restplus API (3 methods). Everything works locally: My webapp runs fine, 3 Rest Api methods respond correctly, and swagger docs are visible/working at /api/documentation.
However on the Production server using mod_wsgi, my webapp runs fine,my rest api works,but if I access the swagger docs at http://mydomain/api/documentation, I get two errors in httpd error log:
Exception occurred processing WSGI script ...
TypeError: expected byte string object for header name, value of type unicode found
EDIT* in access log all requests are 200... the only 500 error is caused by /api/swagger.json... There's even a 200 for /api/documentation which might be why the swagger title and favicon load into the browser tab, but under that is a message saying 'No API Definition Provided'
I am now trying to set the response headers like so (with charset=UTF-8):
resp = app.response_class(
response=json.dumps(found_resource),
status=200,
content_type='application/json; charset=UTF-8'
)
return resp
and
response = app.response_class(
response=json.dumps({'success': False}),
status=400,
content_type='application/json; charset=UTF-8'
)
return response
Am I setting the header to a byte string object correctly for the responses? Any response is either a json dictionary or json list of dictionaries.
I'm a bit confused because this bug report makes it seem that setting the charset is not necessary and possibly redundant using content_type='application/json; charset=UTF-8
python json flask character-encoding mod-wsgi
add a comment |
I have a flask web app running in Production using Python 2.7.5 and mod_wsgi 3.4. The webapp runs fine in production and local loopback.
I recently added a Flask Restplus API (3 methods). Everything works locally: My webapp runs fine, 3 Rest Api methods respond correctly, and swagger docs are visible/working at /api/documentation.
However on the Production server using mod_wsgi, my webapp runs fine,my rest api works,but if I access the swagger docs at http://mydomain/api/documentation, I get two errors in httpd error log:
Exception occurred processing WSGI script ...
TypeError: expected byte string object for header name, value of type unicode found
EDIT* in access log all requests are 200... the only 500 error is caused by /api/swagger.json... There's even a 200 for /api/documentation which might be why the swagger title and favicon load into the browser tab, but under that is a message saying 'No API Definition Provided'
I am now trying to set the response headers like so (with charset=UTF-8):
resp = app.response_class(
response=json.dumps(found_resource),
status=200,
content_type='application/json; charset=UTF-8'
)
return resp
and
response = app.response_class(
response=json.dumps({'success': False}),
status=400,
content_type='application/json; charset=UTF-8'
)
return response
Am I setting the header to a byte string object correctly for the responses? Any response is either a json dictionary or json list of dictionaries.
I'm a bit confused because this bug report makes it seem that setting the charset is not necessary and possibly redundant using content_type='application/json; charset=UTF-8
python json flask character-encoding mod-wsgi
add a comment |
I have a flask web app running in Production using Python 2.7.5 and mod_wsgi 3.4. The webapp runs fine in production and local loopback.
I recently added a Flask Restplus API (3 methods). Everything works locally: My webapp runs fine, 3 Rest Api methods respond correctly, and swagger docs are visible/working at /api/documentation.
However on the Production server using mod_wsgi, my webapp runs fine,my rest api works,but if I access the swagger docs at http://mydomain/api/documentation, I get two errors in httpd error log:
Exception occurred processing WSGI script ...
TypeError: expected byte string object for header name, value of type unicode found
EDIT* in access log all requests are 200... the only 500 error is caused by /api/swagger.json... There's even a 200 for /api/documentation which might be why the swagger title and favicon load into the browser tab, but under that is a message saying 'No API Definition Provided'
I am now trying to set the response headers like so (with charset=UTF-8):
resp = app.response_class(
response=json.dumps(found_resource),
status=200,
content_type='application/json; charset=UTF-8'
)
return resp
and
response = app.response_class(
response=json.dumps({'success': False}),
status=400,
content_type='application/json; charset=UTF-8'
)
return response
Am I setting the header to a byte string object correctly for the responses? Any response is either a json dictionary or json list of dictionaries.
I'm a bit confused because this bug report makes it seem that setting the charset is not necessary and possibly redundant using content_type='application/json; charset=UTF-8
python json flask character-encoding mod-wsgi
I have a flask web app running in Production using Python 2.7.5 and mod_wsgi 3.4. The webapp runs fine in production and local loopback.
I recently added a Flask Restplus API (3 methods). Everything works locally: My webapp runs fine, 3 Rest Api methods respond correctly, and swagger docs are visible/working at /api/documentation.
However on the Production server using mod_wsgi, my webapp runs fine,my rest api works,but if I access the swagger docs at http://mydomain/api/documentation, I get two errors in httpd error log:
Exception occurred processing WSGI script ...
TypeError: expected byte string object for header name, value of type unicode found
EDIT* in access log all requests are 200... the only 500 error is caused by /api/swagger.json... There's even a 200 for /api/documentation which might be why the swagger title and favicon load into the browser tab, but under that is a message saying 'No API Definition Provided'
I am now trying to set the response headers like so (with charset=UTF-8):
resp = app.response_class(
response=json.dumps(found_resource),
status=200,
content_type='application/json; charset=UTF-8'
)
return resp
and
response = app.response_class(
response=json.dumps({'success': False}),
status=400,
content_type='application/json; charset=UTF-8'
)
return response
Am I setting the header to a byte string object correctly for the responses? Any response is either a json dictionary or json list of dictionaries.
I'm a bit confused because this bug report makes it seem that setting the charset is not necessary and possibly redundant using content_type='application/json; charset=UTF-8
python json flask character-encoding mod-wsgi
python json flask character-encoding mod-wsgi
edited Nov 27 '18 at 4:03
sit_on_a_pan_otis
asked Nov 26 '18 at 18:32
sit_on_a_pan_otissit_on_a_pan_otis
31118
31118
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The response needs to be an actual array of bytes instead of a a string. You can use
response = app.response_class(
response=bytes(json.dumps(found_resource).encode('utf-8')),
status=200,
content_type='application/json; charset=UTF-8'
)
return response
If you ever switch to Python 3 you can use bytes(some_string, encoding='utf-8').
Unfortunately I'm still getting the error... "GET /api/swagger.json 500 -- I have it blueprinted to /api/documentation
– sit_on_a_pan_otis
Nov 26 '18 at 21:06
@sit_on_a_pan_otis try the updated thing. Sorry, not super familiar with python2 bytes
– Nick Chapman
Nov 26 '18 at 22:40
will try in the am at work... will give credit to you for sure... as a side note... I also tried app.config['JSON_AS_ASCII'] = False with no luck.
– sit_on_a_pan_otis
Nov 26 '18 at 22:49
add a comment |
Ok in my case... the fix was setting response.headers with Flask's @app.after_request.
Apparently mod_wsgi likes unicode headers, but HTTP expects byte string. Here's the solution... Without it mod_wsgi will not let swagger.json load:
@app.after_request
def after(response):
new_resp_headers = {}
for k, v in response.headers.items():
new_resp_headers[k.encode('ISO-8859-1')] = v.encode('ISO-8859-1')
response.headers = new_resp_headers
return response
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487071%2fmod-wsgi-typeerror-expected-byte-string-object-for-header-name-value-of-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The response needs to be an actual array of bytes instead of a a string. You can use
response = app.response_class(
response=bytes(json.dumps(found_resource).encode('utf-8')),
status=200,
content_type='application/json; charset=UTF-8'
)
return response
If you ever switch to Python 3 you can use bytes(some_string, encoding='utf-8').
Unfortunately I'm still getting the error... "GET /api/swagger.json 500 -- I have it blueprinted to /api/documentation
– sit_on_a_pan_otis
Nov 26 '18 at 21:06
@sit_on_a_pan_otis try the updated thing. Sorry, not super familiar with python2 bytes
– Nick Chapman
Nov 26 '18 at 22:40
will try in the am at work... will give credit to you for sure... as a side note... I also tried app.config['JSON_AS_ASCII'] = False with no luck.
– sit_on_a_pan_otis
Nov 26 '18 at 22:49
add a comment |
The response needs to be an actual array of bytes instead of a a string. You can use
response = app.response_class(
response=bytes(json.dumps(found_resource).encode('utf-8')),
status=200,
content_type='application/json; charset=UTF-8'
)
return response
If you ever switch to Python 3 you can use bytes(some_string, encoding='utf-8').
Unfortunately I'm still getting the error... "GET /api/swagger.json 500 -- I have it blueprinted to /api/documentation
– sit_on_a_pan_otis
Nov 26 '18 at 21:06
@sit_on_a_pan_otis try the updated thing. Sorry, not super familiar with python2 bytes
– Nick Chapman
Nov 26 '18 at 22:40
will try in the am at work... will give credit to you for sure... as a side note... I also tried app.config['JSON_AS_ASCII'] = False with no luck.
– sit_on_a_pan_otis
Nov 26 '18 at 22:49
add a comment |
The response needs to be an actual array of bytes instead of a a string. You can use
response = app.response_class(
response=bytes(json.dumps(found_resource).encode('utf-8')),
status=200,
content_type='application/json; charset=UTF-8'
)
return response
If you ever switch to Python 3 you can use bytes(some_string, encoding='utf-8').
The response needs to be an actual array of bytes instead of a a string. You can use
response = app.response_class(
response=bytes(json.dumps(found_resource).encode('utf-8')),
status=200,
content_type='application/json; charset=UTF-8'
)
return response
If you ever switch to Python 3 you can use bytes(some_string, encoding='utf-8').
edited Nov 26 '18 at 22:40
answered Nov 26 '18 at 18:41
Nick ChapmanNick Chapman
2,3641126
2,3641126
Unfortunately I'm still getting the error... "GET /api/swagger.json 500 -- I have it blueprinted to /api/documentation
– sit_on_a_pan_otis
Nov 26 '18 at 21:06
@sit_on_a_pan_otis try the updated thing. Sorry, not super familiar with python2 bytes
– Nick Chapman
Nov 26 '18 at 22:40
will try in the am at work... will give credit to you for sure... as a side note... I also tried app.config['JSON_AS_ASCII'] = False with no luck.
– sit_on_a_pan_otis
Nov 26 '18 at 22:49
add a comment |
Unfortunately I'm still getting the error... "GET /api/swagger.json 500 -- I have it blueprinted to /api/documentation
– sit_on_a_pan_otis
Nov 26 '18 at 21:06
@sit_on_a_pan_otis try the updated thing. Sorry, not super familiar with python2 bytes
– Nick Chapman
Nov 26 '18 at 22:40
will try in the am at work... will give credit to you for sure... as a side note... I also tried app.config['JSON_AS_ASCII'] = False with no luck.
– sit_on_a_pan_otis
Nov 26 '18 at 22:49
Unfortunately I'm still getting the error... "GET /api/swagger.json 500 -- I have it blueprinted to /api/documentation
– sit_on_a_pan_otis
Nov 26 '18 at 21:06
Unfortunately I'm still getting the error... "GET /api/swagger.json 500 -- I have it blueprinted to /api/documentation
– sit_on_a_pan_otis
Nov 26 '18 at 21:06
@sit_on_a_pan_otis try the updated thing. Sorry, not super familiar with python2 bytes
– Nick Chapman
Nov 26 '18 at 22:40
@sit_on_a_pan_otis try the updated thing. Sorry, not super familiar with python2 bytes
– Nick Chapman
Nov 26 '18 at 22:40
will try in the am at work... will give credit to you for sure... as a side note... I also tried app.config['JSON_AS_ASCII'] = False with no luck.
– sit_on_a_pan_otis
Nov 26 '18 at 22:49
will try in the am at work... will give credit to you for sure... as a side note... I also tried app.config['JSON_AS_ASCII'] = False with no luck.
– sit_on_a_pan_otis
Nov 26 '18 at 22:49
add a comment |
Ok in my case... the fix was setting response.headers with Flask's @app.after_request.
Apparently mod_wsgi likes unicode headers, but HTTP expects byte string. Here's the solution... Without it mod_wsgi will not let swagger.json load:
@app.after_request
def after(response):
new_resp_headers = {}
for k, v in response.headers.items():
new_resp_headers[k.encode('ISO-8859-1')] = v.encode('ISO-8859-1')
response.headers = new_resp_headers
return response
add a comment |
Ok in my case... the fix was setting response.headers with Flask's @app.after_request.
Apparently mod_wsgi likes unicode headers, but HTTP expects byte string. Here's the solution... Without it mod_wsgi will not let swagger.json load:
@app.after_request
def after(response):
new_resp_headers = {}
for k, v in response.headers.items():
new_resp_headers[k.encode('ISO-8859-1')] = v.encode('ISO-8859-1')
response.headers = new_resp_headers
return response
add a comment |
Ok in my case... the fix was setting response.headers with Flask's @app.after_request.
Apparently mod_wsgi likes unicode headers, but HTTP expects byte string. Here's the solution... Without it mod_wsgi will not let swagger.json load:
@app.after_request
def after(response):
new_resp_headers = {}
for k, v in response.headers.items():
new_resp_headers[k.encode('ISO-8859-1')] = v.encode('ISO-8859-1')
response.headers = new_resp_headers
return response
Ok in my case... the fix was setting response.headers with Flask's @app.after_request.
Apparently mod_wsgi likes unicode headers, but HTTP expects byte string. Here's the solution... Without it mod_wsgi will not let swagger.json load:
@app.after_request
def after(response):
new_resp_headers = {}
for k, v in response.headers.items():
new_resp_headers[k.encode('ISO-8859-1')] = v.encode('ISO-8859-1')
response.headers = new_resp_headers
return response
answered Nov 28 '18 at 18:16
sit_on_a_pan_otissit_on_a_pan_otis
31118
31118
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487071%2fmod-wsgi-typeerror-expected-byte-string-object-for-header-name-value-of-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown