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

specifying charset for body content? #926

Open
garretwilson opened this issue Oct 18, 2017 · 3 comments
Open

specifying charset for body content? #926

garretwilson opened this issue Oct 18, 2017 · 3 comments

Comments

@garretwilson
Copy link

What charset do methods such as RequestSpecification.body(String body) use? Because you have to be using some charset to convert a string to bytes.

Surely you aren't using the default system charset --- this would be a defect, as tests would not be reliable nor repeatable because you have no idea what charset the system is using.

From the API it seems that there is a big problem here --- you should be allowing the user to indicate the charset to use.

@garretwilson
Copy link
Author

Unfortunately I'm getting the idea that this is indeed a defect, after reading e.g. #567 .

Look at all the Java APIs from String.getBytes(Charset) to InputStreamReader. They all require a Charset to be indicated. Only old APIs rely on the system default charset, because this is not reliable across platforms.

Ideally you should have a RequestSpecification.charset(Charset) method, but you should also pick up whether the charset parameter was used in RequestSpecification.contentType(ContentType contentType). Either of these would be used as the charset for RequestSpecification.body(String body); otherwise, it should throw an exception if the charset hasn't been set first.

Having a RequestSpecification.body(String body, Charset charset) would allow the body and charset to be set in one go.

@garretwilson
Copy link
Author

To better explain the problem, I wonder what charset this would use to encode the bytes?

when()
  .contentType("text/plain; charset=ISO-2022-JP")
  .body("もしもし")
  …

Would the body be encoded in ISO-2022-JP? Or UTF-8? (How would I know? The documentation doesn't seem clear on this point.) My fear is that neither would happen, and that REST Assured might in fact just use the system default charset---which might be Cp1251 for all we know (as @mkotsur might be using in mkotsur/restito#66 ).

@johanhaleby
Copy link
Collaborator

If you specify a charset explicitly in the contentType that charset should be used.

If charset is not explicitly specified REST Assured will query the config to check which charset to use. You can specify either the default charset (which defaults to ISO_8859_1 all content-types (except JSON which using UTF-8) as defined by Apache Http Client in HTTP#DEF_CONTENT_CHARSET) or the charset on a per contet-type basis using the EncoderConfig.

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

2 participants