ducaale / xh

Friendly and fast tool for sending HTTP requests
MIT License
5.58k stars 98 forks source link

Feature request: decode utf-8 encoded string #309

Closed XYenon closed 7 months ago

XYenon commented 1 year ago

HTTPie will decode utf-8 encoded string in json, but xh don't

https POST httpbin.org/post 'test=测试'

{
    ...
    "json": {
        "test": "测试"
    },
    ...
}

xhs POST httpbin.org/post 'test=测试'

{
    ...
    "json": {
        "test": "\u6d4b\u8bd5"
    },
    ...
}
blyxxyz commented 1 year ago

Agreed! Showing that JSON as is is not very readable.

We should probably move away from jsonxf and use serde_json instead. With the right tricks we can format the JSON data while it's decoded. (I experimented with this a few months ago.) We could even add colors without syntect, for a huge speedup.

A hard part is error handling, particularly while streaming. If the response turns out to be invalid midway through we have to pick up exactly where serde_json's decoder left off to write out the rest without dropping or repeating anything. (HTTPie "solves" this by assuming that --stream means each line is its own JSON document but that can look bad, try http --stream httpbin.org/get.)

We'd have to keep using syntect to color invalid JSON (and probably unformatted JSON? Maybe serde_json lets you look at whitespace so we can pass it through, IDK).

ducaale commented 1 year ago

A hard part is error handling, particularly while streaming. If the response turns out to be invalid midway through we have to pick up exactly where serde_json's decoder left off to write out the rest without dropping or repeating anything. (HTTPie "solves" this by assuming that --stream means each line is its own JSON document but that can look bad, try http --stream httpbin.org/get.)

I wonder if serde_json::StreamDeserializer would help in this case? You can use byte_offset to check how many bytes have been read so far.