Pm.response.text() can't work properly with chinese

pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("健康");
});

When I use the code above to test weather the response contains the chinese word “健康”,the test fails(The response actually contains the word). I have tried to google for the solutions, but can’t work it out. Hope someone can help.

curl:
curl --location ‘https://www.v2ex.com/api/topics/hot.json’ \

–header ‘Content-Type: application/json;charset=UTF-8’

Hey @wodefan :wave:

Welcome to the Postman community :postman:

Your response is returning JSON, is there a reason you’re trying to assert against the whole response as a string and not target the specific property?

Something like this:

pm.test("Body matches string", function () {
    pm.expect(pm.response.json()[0].node.title).to.equal("健康");
});

@danny-dainton Thank you for reply.

The example code you post can work properly. I actually know the usage.

I just wonder why pm.expect(pm.response.text()).to.include("健康") doesn’t work, cause I’m learning Postman.

If the more targeted way works and it’s more effective, I’m not sure why the other way would be required.

Without more information or looking deeper into this, I can’t say why that’s not working for you.

When I console log pm.response.text(), I can see that the Chinese characters are being transposed to the Unicode characters.

So your node.header element for example is showing…

"\u4e00\u4e2a\u66f4\u597d\u7684\u4e16\u754c\u9700\u8981\u4f60\u6301\u7eed\u5730\u63d0\u51fa\u597d\u95ee\u9898\u3002"

Instead of…

"header": "一个更好的世界需要你持续地提出好问题。"

Therefore technically the test is correct. That text is not showing in the parsed response.

When you parse it to JSON, it doesn’t transpose the characters, which is why that is working as expected.

@michaelderekjones You’re right!

The question is that I want to use pm.expect(pm.response.text()).to.include(“健康”) because of its convenience(While using pm.response.json(), I have to designate the exact node by using dot (.) notation).

Is there a solution pm.response.text() can work with Chinese?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.