Skip to content
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

HAR PostData text is wrong type #245

Closed
jba opened this issue May 25, 2018 · 2 comments · Fixed by #246
Closed

HAR PostData text is wrong type #245

jba opened this issue May 25, 2018 · 2 comments · Fixed by #246

Comments

@jba
Copy link
Collaborator

jba commented May 25, 2018

har.PostData.Text, the body of a POST, has type string. This is a mistake. It should be []byte.

The HAR spec does say that text is a string, but it means a JSON string, not a Go string. POST data can contain arbitrary bytes (e.g. if the MIME type is application/octet-stream).

The reason this matters is that encoding/json.Marshal expects strings to be valid UTF-8; if they are not, it loses information by replacing invalid code points with the replacement character. So when a har.Log is marshaled to JSON, the result is wrong.

Example:

    pd := &har.PostData{Text: string([]byte{150, 151, 152})}
    bytes, err := json.Marshal(pd)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%v\n", string(bytes))

Output:

{"mimeType":"","params":null,"text":"\ufffd\ufffd\ufffd"}
@jba
Copy link
Collaborator Author

jba commented May 25, 2018

I'm happy to provide a PR if you're OK with this (it is a breaking change).

@bramhaghosh
Copy link
Member

bramhaghosh commented May 25, 2018 via email

jba added a commit that referenced this issue May 26, 2018
Previously, it was string, which doesn't correctly round-trip with
encoding/json because that package requires strings to be valid UTF-8.

The right type is []byte, since post data can be arbitrary bytes.

Fixes #245.
@jba jba closed this as completed in #246 May 30, 2018
jba added a commit that referenced this issue May 30, 2018
Previously, post data did not round-trip with encoding/json because that package requires strings to be valid UTF-8, and post data can be  arbitrary bytes.

Following the suggestion in
https://groups.google.com/forum/#!topic/http-archive-specification/9wYrqin3Fsc/discussion,
binary (that is, invalid UTF-8) post data is implemented by base64-encoding the data in the "text" key, and adding an "encoding" key equal to "base64".

Fixes #245.
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

Successfully merging a pull request may close this issue.

2 participants