Skip to content

Failure encoding utf-8 data in json POST #28

@myndzi

Description

@myndzi

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.

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