由於Bottle Web框架,我有一個在Python中實現的HTTP/JSON Restful服務器。我想將Gzip發送給客戶端的數據。Httpie無法解碼我的Bottle API Gzipped響應
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# curl -H "Content-Type: application/json" -X POST -d '{"key1": 1, "key2": 2}' http://localhost:6789/post
#
from bottle import run, request, post, route, response
from zlib import compress
import json
data = {'my': 'json'}
@post('/post')
def api_post():
global data
data = json.loads(request.body.read())
return(data)
@route('/get')
def api_get():
global data
response.headers['Content-Encoding'] = 'identity'
return(json.dumps(data).encode('utf-8'))
@route('/getgzip')
def api_get_gzip():
global data
if 'gzip' in request.headers.get('Accept-Encoding', ''):
response.headers['Content-Encoding'] = 'gzip'
ret = compress(json.dumps(data).encode('utf-8'))
else:
response.headers['Content-Encoding'] = 'identity'
ret = json.dumps(data).encode('utf-8')
return(ret)
run(host='localhost', port=6789, debug=True)
當我測試用捲曲的服務器,結果是好(如果我使用--compressed選項標籤):
$ curl -H "Accept-encoding: gzip, deflated" -v --compressed http://localhost:6789/getgzip
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 6789 (#0)
> GET /getgzip HTTP/1.1
> Host: localhost:6789
> User-Agent: curl/7.47.0
> Accept: */*
> Accept-encoding: gzip, deflated
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Date: Sun, 12 Nov 2017 09:09:09 GMT
< Server: WSGIServer/0.1 Python/2.7.12
< Content-Length: 22
< Content-Encoding: gzip
< Content-Type: text/html; charset=UTF-8
<
* Closing connection 0
{"my": "json"}
但不能與HTTPie(或Firefox或Chrome .. 。):
$ http http://localhost:6789/getgzipHTTP/1.0 200 OK
Content-Encoding: gzip
Content-Length: 22
Content-Type: text/html; charset=UTF-8
Date: Sun, 12 Nov 2017 09:10:10 GMT
Server: WSGIServer/0.1 Python/2.7.12
http: error: ContentDecodingError: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing: incorrect header check',))
任何想法?