Can I POST data with python requests lib with http-gzip or deflate compression?

后端 未结 5 1769
深忆病人
深忆病人 2021-02-13 03:39

I use the request-module of python 2.7 to post a bigger chunk of data to a service I can\'t change. Since the data is mostly text, it is large but would compress quite well. The

5条回答
  •  终归单人心
    2021-02-13 04:33

    I've tested the solution proposed by Robᵩ with some modifications and it works.

    PSEUDOCODE (sorry I've extrapolated it from my code so I had to cut out some parts and haven't tested, anyway you can get your idea)

    additional_headers['content-encoding'] = 'gzip'
    s = StringIO.StringIO()
    g = gzip.GzipFile(fileobj=s, mode='w')
    g.write(json_body)
    g.close()
    gzipped_body = s.getvalue()
    request_body = gzipped_body
    
    r = requests.post(endpoint_url, data=request_body, headers=additional_headers)
    

提交回复
热议问题