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

set JSON request body encoding to UTF-8 (it was ISO_8859_1 by default) #1

Merged
merged 1 commit into from
Jan 12, 2024

Conversation

D3DXInitPerDX
Copy link

@D3DXInitPerDX D3DXInitPerDX commented Jan 12, 2024

per JSON specification JSON strings SHALL be encoded in UTF-8 (or other UTF encodings).
the constructor StringEntity(String) uses ISO_8859_1 encoding by default. This mistake leads to the error from szurubooru api: "Could not decode the request body. The JSON was incorrect or was not encoded as UTF-8" if json contains unusual symbols (like ×).

(the latest release does not implement any API that could be broken by this bug, I encountered it with my extended SzurubooruAPI that creates post with pre-defined tags and source:

public SzurubooruPost createPost(FileToken content, String safety, List<String> tags, String source) throws IOException, SzurubooruHTTPException, SzurubooruResourceNotSynchronizedException {
            if (!SzurubooruPost.isValidSafety(safety)) {
                throw new IllegalArgumentException("Safety must be of value safe, sketchy or unsafe");
            } else {

                List<Map<String, Object>> tagsMap = tags.stream()
                        .map(this::truncateTag)
                        .map(x -> Map.of("names", (Object) List.of(x)))
                        .toList();
                SzurubooruPost post = new SzurubooruPost(this, new HashMap<>());
                post.setNewJson(Map.of("tags", tagsMap, "safety", safety, "contentToken", content.getToken(),
                        "source", source));
                post.push();
                return post;
            }
        }

        // szurubooru does not accept tags longer than 128 in length
        private String truncateTag(String tag) {
            return tag.length() > 128 ? tag.substring(0, 128) : tag;
        }

So, if any tag or the source contains "×", the mentioned error will occur. This commit fixes that.

@D3DXInitPerDX
Copy link
Author

@G1org1owo please look at this merge request.

@G1org1owo G1org1owo self-requested a review January 12, 2024 23:50
@G1org1owo G1org1owo merged commit 78235db into G1org1owo:main Jan 12, 2024
@D3DXInitPerDX D3DXInitPerDX deleted the fix/json-encoding branch January 12, 2024 23:55
@G1org1owo
Copy link
Owner

Thanks for the contribution! I didn't think someone would actually use this lib so you've made me quite happy :)

If there is anything else that needs fixing/improving I'm open to ideas!

@D3DXInitPerDX
Copy link
Author

Glad I could help. Right now i'm using only a small fraction of the API (my self-hosted szuru is pretty simple structurally), so no any good ideas regarding the functionality. Maybe later I'll think of something.

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 this pull request may close these issues.

2 participants