Skip to content

API Error: Your request contained invalid JSON #118

@comiluv

Description

@comiluv

I use Korean locale Windows 11. In my environment, if I send an input containing Korean language, an error message similar to following is shown
Your request contained invalid JSON: 'utf-8' codec can't decode byte 0xbe in position 43: invalid start byte

If the user uses US encoding (CP437) in Windows and the response contains characters that can't be converted to US encoding, the message can be broken to ?? characters as shown in the screenshot:
화면 캡처 2023-03-23 103556

This issue is simliar to #106 and #114 as the issue is that default Windows encoding may not support characters in the messages. If I apply the suggested workaround described in those issue pages which is to call vim.iconv() on the result of vim.fn.json_encode() function before feeding it to curl, I had encountered problems where continued chat session would be broken because the workaround would convert the encoding repeatedly on the json message each time Api.make_call() function is called.

My proposed fix for this issue is to use a temporary message file. I have successfully tested this on my fork in https://github.com/comiluv/ChatGPT.nvim/commit/dfb06fb0fd9cf899e06deca426fc8f9d1c6f37e5. In this way, because Neovim always uses UTF-8 encoding internally, the temporary file will be guaranteed to be encoded in UTF-8 regardless of what the user's OS uses for encoding. This solution also prevents the problem where the same encoding conversion can be applied multiple times on the message which other workaround can cause, possibly resulting in a garbled message.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions