Getting Failed to decode gzip: Failed to decompress data: gzip: invalid header

I’m stuck on the last stage of gzip compression
Here are my logs:

remote: [your_program] message:apple
remote: [your_program] compressed_message=b'\x1f\x8b\x08\x00s\x84fg\x02\xffK,(\xc8I
\x05\x00P\xd0.\xa9\x05\x00\x00\x00'
remote: [your_program] decoded:1f8b08007384666702ff4b2c28c849050050d02ea905000000  
remote: [your_program] length_of_compressed:25
remote: [your_program] response to send: HTTP/1.1 200 OK
remote: [your_program] Content-Encoding: gzip
remote: [your_program] Content-Length: 25
remote: [your_program] 
remote: [your_program] b'\x1f\x8b\x08\x00s\x84fg\x02\xffK,(\xc8I\x05\x00P\xd0.\xa9\
x05\x00\x00\x00'
remote: [tester::#CR8] Received response with 200 status code
remote: [tester::#CR8] Γ£ô Content-Encoding header is present
remote: [tester::#CR8] Γ£ô Content-Length header is present
remote: [tester::#CR8] Failed to decode gzip: Failed to decompress data: gzip: invalid header

include relevant logs here (please make sure to keep the backticks around this!)

And here’s a snippet of my code:

def handle_client(client_sock):
    if client_sock not in connected_client:
        connected_client.append(client_sock)
    else:
        print('client is already in connected list')

    while True:

        request = client_sock.recv(4096).decode()
        if not request:
            break
        # debugging purposes
        print(f'{request}')
        response = parse_request(request)
        client_sock.sendall(response.encode())

def parse_request(request):
     #some parts were cut out 
    elif request.split()[path].startswith(f'/echo/{filename}') and request.split()[0] == 'GET':
        value = request.split()[1][6:]
        gzip_header_present = accept_gzip(request)
        if gzip_header_present:
            return gzip_header_present
        else:
            return f'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length:{len(value)}\r\n\r\n{value}'

def accept_gzip(request):
    body = request.split()[1][6:]
    print(f'message:{body}')
    headers = parse_headers(request)
    if 'Accept-Encoding' in headers:
        if 'gzip' in headers['Accept-Encoding']:
            compressed = gzip.compress(body.encode())
            print(f'compressed_message={compressed}')
            print(f'decoded:{compressed.hex()}')
            print(f'length_of_compressed:{len(compressed)}')
            res = (
                'HTTP/1.1 200 OK\r\n'
                'Content-Encoding: gzip\r\n'
                f'Content-Length: {len(compressed)}\r\n'
                '\r\n'
                f'{compressed.decode()}'
            )
            print(f"response to send: {res}")
            return res
    return None

Hi @Fruitpunch44, looks like you’ve got past this stage — do you happen to remember what was wrong? Would love to see if we can improve the tester / instructions.

Discovered that the main problem was how the compressed gzip was being sent ,i was initially converting the gzip to a string type and sending the response with .encode() and that was what gave me the error.
So i just sent the gzip directly has bytes and that solved the problem for me.

1 Like