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

[fix] I can not get query parameters about Korean #1787

Closed
3 tasks done
shaan-lee opened this issue Nov 6, 2023 · 3 comments
Closed
3 tasks done

[fix] I can not get query parameters about Korean #1787

shaan-lee opened this issue Nov 6, 2023 · 3 comments
Labels

Comments

@shaan-lee
Copy link

shaan-lee commented Nov 6, 2023

Describe the bug

Node.js version:
v16.20.2

OS version:
mac m2
mac OS 13.6.1

Description:

Actual behavior

when i request to my localhost in GET method, include url parameters
example) http://localhost:4000/api/query?name=한글

in server output of ctx.query is { name: 'í\x95\x9Cê¸\x80' }
how can i convert it to natural string?

Expected behavior

Code to reproduce

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
@shaan-lee shaan-lee added the bug label Nov 6, 2023
@shaan-lee
Copy link
Author

i am useing below dependency in server

import Koa from "koa";
import Router from "koa-router";
import bodyParser from "koa-bodyparser";
import json from "koa-json";

@lagden
Copy link
Contributor

lagden commented Nov 7, 2023

@shaan-lee

Inside the middleware method

function sample(ctx) {
  const {name} = ctx.query

  const uint8 = Uint8Array.from([...name].map(c => c.codePointAt(0)))
  const str = new TextDecoder().decode(uint8)

  console.log({uint8, str})
  // => { uint8: Uint8Array(6) [ 237, 149, 156, 234, 184, 128 ], str: '한글' }

  ctx.body = str
}

Hope this helps! 😉

@shaan-lee
Copy link
Author

thank you, i solve the problem below code

app.use(async (ctx: Koa.Context, next: Koa.Next) => {
    const query = ctx.query;
    for (const q in query) {
        const queryValue = String(query[q]);
        const queryCodePoint = Uint8Array.from([...queryValue]
            .map(c => Number(c.codePointAt(0))));
        const queryStr = new TextDecoder().decode(queryCodePoint);
        query[q] = queryStr;
    }
    ctx.query = query;
    await next();
})

your code was very helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants