Skip to content

GZIP encoding support #432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ghost opened this issue Apr 23, 2020 · 3 comments
Open

GZIP encoding support #432

ghost opened this issue Apr 23, 2020 · 3 comments

Comments

@ghost
Copy link

ghost commented Apr 23, 2020

Hi,

first of all thanks for your work on keeping VBA Web up-to-date, you are doing a great job. I'd like to follow up on an old item (#61) on gzip encoding. I have integrated the decoding for large JSON files in my application as I needed it, and thought I share what I did and give some input for further improving VBA-Web

First of all I found a module that can decode GZIP on Stack Overflow, that does the real work (see here: https://stackoverflow.com/questions/58026702/how-to-decompress-http-responses-in-vba-excel). The function "inflate" takes a GZIP byte array and transforms it unicode. Unfortunately it can only decode gzip, but not encode.

To integrate it with VBA Web, this is how I created the request:

    Dim Request As New WebRequest
    Dim Response As New WebResponse
    
    Request.Resource = "/function"
    Request.AddHeader "Accept-Encoding", "gzip"
    
    Request.RequestFormat = Custom
    Request.CustomResponseFormat = "gzip"
    
    WebHelpers.RegisterConverter "gzip", "application/json", "GzipConverter.ConvertToGzip", "GzipConverter.ParseGzip", , "Binary"
       
    Set Response = Client.Execute(Request)

I've then created a function "ParseGzip" in one the module GzipConverter that will receive the binary (that's important!) body from the requst and decode it.

Public Function ParseGzip(Bytes As Variant) As Object

    Dim byteArray() As Byte
    Dim strResponse As String
    byteArray = Bytes ' convert variant array to byte array
        
    Call Inflate(byteArray) 'decode the GZIP byte array
    strResponse = StrConv(byteArray, vbUnicode) 'create a string from the encoded byte array
    
    Set ParseGzip= ParseJson(strResponse) 'parse string to json

End Function

This functions returns the parsed JSON to the VBA web for further processing. This works, but it is a all bit hacky. Although proposed as a solution in #61, the RegisterConverter function is not really optimal here, because gzip is not an content type (in my case I am working with JSON). If the server does not return gzip (which is visibile in the encoding header of the response), it would still try to parse it as gzip and would through an error.

I think it would make sense to allow an additional hook for an encoding function, which is triggered by
content-encoding: gzip
in the header. Would love to see a more proper intergration into VBA Web...

@zgrose
Copy link

zgrose commented Apr 23, 2020

Create a pull-request with your code added to the class(es)?

@ghost
Copy link
Author

ghost commented Apr 23, 2020 via email

@ghost
Copy link
Author

ghost commented Apr 24, 2020

Well, I found a better solution that integrates it properly and created a pull request #433 (first time done something like this, hope everything worked).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant