Katalon Studio v8.1.0 bundles the Apache HttpClient library. You can check it in a <projectDir>/.classpath
file. org.apache.httpcomponents.httpclient_4.5.1.jar
is available for a test cases script.
I added a Test Case into the aforementioned sample project “web-service-tests”. It is a HTTP client that sends a POST request with Content-Type: multipart/form-data. The text parts are encoded in charset=utf-8.
web-service-tests.zip (136.9 KB)
// original: https://www.tutorialspoint.com/apache_httpclient/apache_httpclient_response_handlers.htm
import java.nio.charset.StandardCharsets
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
CloseableHttpClient httpclient = HttpClients.createDefault()
File file = new File("./server.groovy")
StringBody part_in_us_ascii = new StringBody("hello,world", new ContentType("text/plain", StandardCharsets.UTF_8))
StringBody part_in_germany = new StringBody("Grüß Gott!", new ContentType("text/plain", StandardCharsets.UTF_8))
StringBody part_in_japanese = new StringBody("こんにちは", new ContentType("text/plain", StandardCharsets.UTF_8))
// build multipart upload request
HttpEntity data = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addPart("part-in-us-ascii", part_in_us_ascii)
.addPart("part-in-germany", part_in_germany)
.addPart("part-in-japanese", part_in_japanese)
.build()
// build http request and assign multipart upload data
HttpUriRequest request = RequestBuilder
.post("http://localhost:80")
.setEntity(data)
.build()
println("Executing request ${request.getRequestLine()}")
// Create a custom response handler
ResponseHandler<String> responseHandler = new MyResponseHander()
String responseBody = httpclient.execute(request, responseHandler)
System.out.println("----------------------------------------")
System.out.println(responseBody)
class MyResponseHander implements ResponseHandler<String> {
public String handleResponse(final HttpResponse response) throws IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
}
When I tried this against the http://localhost:80 server, it printed the following output in the console.
method = POST
uri = /
body = --mNPsR4gxoIl39TteGuiMPP5cw0S7NeoSu6h8dvki
Content-Disposition: form-data; name="part-in-us-ascii"
hello,world
--mNPsR4gxoIl39TteGuiMPP5cw0S7NeoSu6h8dvki
Content-Disposition: form-data; name="part-in-germany"
Grüß Gott!
--mNPsR4gxoIl39TteGuiMPP5cw0S7NeoSu6h8dvki
Content-Disposition: form-data; name="part-in-japanese"
こんにちは
--mNPsR4gxoIl39TteGuiMPP5cw0S7NeoSu6h8dvki--
As you can see, German characters and Japanese Characters are properly encoded&decoded.
Programming note
See the following line of the “http client”.
StringBody part_in_germany = new StringBody(
"Grüß Gott!",
new ContentType("text/plain",
StandardCharsets.UTF_8))
The request has with 3 parts. Each of parts are “text/plain”, is encoded in utf-8. This line explicitly specifies which encoding is used for each parts. If 3 parts are encoded in ISO-8859-1, utf-8, Shift_JIS, then the code should look like the following:
StringBody part_in_us_ascii = new StringBody("hello,world", ...
StandardCharsets.ISO_ISO8859_1))
StringBody part_in_germany = new StringBody("Grüß Gott!", ...
StandardCharsets.UTF_8))
StringBody part_in_japanese = new StringBody("こんにちは", ...
Charset.forName("Shift-JIS")))
Apache httpclient library encodes a string of a part of a Request of “multipart/form-data”
into bytes using the charset specified for each part.
As far as I see in the current version of the source code of Katalon Studio, it does not encode the string into bytes using charsets specified per each parts. It encodes whole of a request body in UNICODE into a byte array using charset=US-ASCII. Therefore non US-ASCII characters are garbled.