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

Failure encoding utf-8 data in json POST #28

Open
myndzi opened this issue Dec 28, 2016 · 2 comments
Open

Failure encoding utf-8 data in json POST #28

myndzi opened this issue Dec 28, 2016 · 2 comments

Comments

@myndzi
Copy link

myndzi commented Dec 28, 2016

I wrote a script to bulk upload a bunch of files to an API. One of the files was named yellow-café-au-lait-bowls-vintage_3.tif. The accented 'e' caused the API to return an error parsing the JSON.

The following addition to a helper function resolved the problem:

        if (method === 'get') {
            return bhttp[method](`${config.apiBase}${path}${query}`, opts);
        } else if (method === 'post') {
            return bhttp[method](`${config.apiBase}${path}`, params, opts);
        } else if (method === 'rawpost') {
            opts.method = 'POST';
            opts.encodeJSON = false;
            opts.inputBuffer = Buffer.from(JSON.stringify(params));
            opts.headers['Content-Type'] = 'application/json';
            return bhttp.request(`${config.apiBase}${path}`, opts);
        }

As far as I can tell, something about the post helper method being passed an object with utf-8 text in the value is causing problems. The API is https so I can't easily sniff the data; if this is enough info to go on then great. If you need more info, let me know what could help.

@myndzi
Copy link
Author

myndzi commented Dec 28, 2016

Further investigation shows it to be a problem calculating the length of the content. I think this has also caused problems with some other files I was trying to upload, in the other direction (they don't appear to conclude their upload).
Edit: The file-upload bits are using streams, so not sure what that's about and may or may not be related to bhttp; I also can't seem to upload them from the browser webface so I'd ignore this bit for now.

The failing request is missing the "} from the end of the JSON data, while the successful request is not. I assume this is because bhttp (correctly) only writes the number of bytes that it says it will write, but (incorrectly) miscalculates the bytes to be the length of the UTF-8 string in code points or something like that. Could probably be fixed by first converting to a buffer and checking the length of the buffer.

@myndzi
Copy link
Author

myndzi commented Dec 28, 2016

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