Re: java.nio.charset.UnsupportedCharsetException: json error

Dmitri T <[email protected]> Wed, 29 Nov 2023 17:49:37 +0100
Newsgroups gmane.comp.jakarta.jmeter.user
Message-ID <PH7PR12MB713979A3884CC30DD04485D9C083A@PH7PR12MB7139.namprd12.prod.outlook.com>
Vadim Avkhimenia wrote:
> Hello,
>
> I am using Jmeter 5.6.2 to see how my REST API will respond to multiple
> requests.
>
> I am trying to replicate the request I made with the python requests
> library. The request I am trying to replicate is:
>
> import requests
> import json
> requests.get('http://[<ipv6>]:<port number>/<endpoint>',
> data=json.dumps({'key' : ['value']}))
>
> The REST API I have setup returns the output in the format {'key' :
> 'value'}.
>
> To try to load test I setup an HTTP Request screen where I filled in the
> protocol, server name, port number, path, and content encoding. Content
> encoding was set as 'json' but I've tried not setting this field too. In
> the 'Body Data' section I pasted '{"key": ["value"]}' . I've also setup a
> header manager screen with 'Content-Type' of 'application/json'. I've also
> setup a JSON Extractor screen and left everything empty in it, but have
> tried in the past to populate the 'Names of created variables' field with
> 'key'.
>
> When I run my test, the 'View Results in Tree' produces these outputs:
>
> *Sampler result:*
>
> Thread Name:Thread Group Matmul 1-1
> Sample Start:2023-11-29 08:58:08 MST
> Load time:0
> Connect Time:0
> Latency:0
> Size in bytes:1056
> Sent bytes:0
> Headers size in bytes:0
> Body size in bytes:1056
> Sample Count:1
> Error Count:1
> Data type ("text"|"bin"|""):text
> Response code:Non HTTP response code:
> java.nio.charset.UnsupportedCharsetException
> Response message:Non HTTP response message: json
>
>
> HTTPSampleResult fields:
> ContentType:
> DataEncoding: null
>
> *Request:*
>
> GET http://[<ipv6>]:<port number>/<endpoint>
>
> GET data:
>
>
> [no cookies]
>
> *Response Body:*
>
> java.nio.charset.UnsupportedCharsetException: json
> at java.base/java.nio.charset.Charset.forName(Charset.java:529)
> at
> org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.setupHttpEntityEnclosingRequestData(HTTPHC4Impl.java:1543)
> at
> org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.handleMethod(HTTPHC4Impl.java:891)
> at
> org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:646)
> at
> org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:66)
> at
> org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1311)
> at
> org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1300)
> at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:651)
> at
> org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:570)
> at
> org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:501)
> at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:268)
> at java.base/java.lang.Thread.run(Thread.java:834)
>
> *Response headers:*
>
> BLANK
>
> My understanding is that the request is sent successfully, because when I
> run the test the button in top right first turns green and then turns red,
> but the response can't be parsed because it's in the json format.
>
> Should the JSON Extractor not be able to take care of this?
>
> Please advise.
>
> Thank you.
>
> Regards,
>
> Vadim
>
You should either leave content encoding blank or use a valid charset 
<https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html> 
like UTF-8 <https://en.wikipedia.org/wiki/UTF-8> for this.

Also you can just record your Python script using JMeter's HTTP(S) Test 
Script Recorder 
<https://guide.blazemeter.com/hc/en-us/articles/206733979-Recording-a-user-session-with-the-proxy>, 
amend your code to look like:

jmeter_proxy = {
    'http': 'http://localhost:8888',
    'https': 'http://localhost:8888',
}

requests.get('http://[<ipv6>]:<port number>/<endpoint>',
data=json.dumps({'key' : ['value']}), proxies=jmeter_proxy)