Spring Boot encoding issue ISO instead of UTF-8 - java

Spring Boot is decoding a String from a #RequestHeader annotation with ISO-8859-1 instead of UTF-8.
abstract of application.properties
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.http.encoding.force-request=true
spring.http.encoding.force-response=true
spring.messages.encoding=UTF-8
spring.banner.charset=UTF-8
spring.mandatory-file-encoding=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.freemarker.charset=UTF-8
Controller
#Controller
#RequestMapping(value = "/mapping/", produces = "text/plain;charset=UTF-8")
public class MyController {}
curl command
curl 'https://example.com/mymapping/' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Referer: https://example.com/mymapping/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9,de;q=0.8' -H 'Cookie: cookiesession1=<*>; mellon-cookie=<*>; JSESSIONID=<*>' --compressed
Some special characters are still wrong decoded for example a ü will be ü afterwards.
With the following workaround I get the correct string.
private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final Charset ISO = Charset.forName("ISO-8859-1");
String textnew = new String(textold.getBytes(ISO), UTF_8);
I want to declare my application to decode all data in UTF-8 not ISO or something similiar. Any suggestions?

Somewhere i read Java expects that *.properties files are encoded in ISO-8859-1 and that's why Spring treats application.properties as if it's in ISO-8859-1.Try using YAML instead of properties files.

Can you please verify the response headers what is the value for Content-Type:?
for ex :application/json;charset=UTF-8

Related

How to accept json, xml, x-www-form-urlencoded in openapi driven spring boot application?

I'm implementing a petstore server in spring using the gradle plugin to generate classes for the API:
https://github.com/swagger-api/swagger-petstore/blob/swagger-petstore-v3-1.0.5/src/main/resources/openapi.yaml
An example for such a generated API code is
...
public interface UserApi {
...
#PostMapping(
value = "/user",
produces = { "application/json", "application/xml" },
consumes = { "application/json", "application/xml", "application/x-www-form-urlencoded" }
)
default ResponseEntity<User> createUser(#ApiParam(value = "Created user object" ) #Valid #RequestBody(required = false) User user) {
....
For a minimal example I don't even overwrite the above method.
With springdoc I can create a swagger ui page at http://localhost:8080/swagger-ui.html that is generated from the code.
There, I can generate curl requests for all media types offered y the api:
curl -X POST "http://localhost:8080/user" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"id\":0,\"username\":\"string\",\"firstName\":\"string\",\"lastName\":\"string\",\"email\":\"string\",\"password\":\"string\",\"phone\":\"string\",\"userStatus\":0}"
curl -X POST "http://localhost:8080/user" -H "accept: application/json" -H "Content-Type: application/xml" -d "<?xml version=\"1.0\" encoding=\"UTF-8\"?><User>\t<id>0</id>\t<username>string</username>\t<firstName>string</firstName>\t<lastName>string</lastName>\t<email>string</email>\t<password>string</password>\t<phone>string</phone>\t<userStatus>0</userStatus></User>"
curl -X POST "http://localhost:8080/user" -H "accept: application/json" -H "Content-Type: application/x-www-form-urlencoded" -d "id=0&username=string&firstName=string&lastName=string&email=string&password=string&phone=string&userStatus=0"
The first(json) works, but for xml and x-www-form-urlencoded
I get:
{"timestamp":"2020-10-19T22:42:43.215+00:00","status":415,"error":"Unsupported Media Type","message":"","path":"/user"}
That response code is not in the generated default implementation, it must be thrown in the steps before.
There are similar questions already on stackoverflow, but through openapi I cannot just leave out annotations. Is there a way for me to implement a request accepting all three media types using openapi and spring boot?

Spring boot: header value encoding

curl 'http://localhost:8080/userlogin'
-H 'Accept-Encoding: gzip, deflate, br'
-H 'GICAR: UNITAT_MAJOR=PRESIDÈNCIA'
As you can see, browser is sending a header with a content with È character.
I need to deal with it in to service:
LOG.debug("Default Cahrset: {}", Charset.defaultCharset().displayName());
String headerValue = request.getHeader(EspaiDocConstants.Headers.GICAR_HEADER);
LOG.debug("Header value: {}", headerValue);
The output is:
Default Cahrset: UTF-8
UNITAT_MAJOR=PRESIDÃNCIA
As you can see, È is chaged to Ã.
Any ideas?
Try to add this to the curl command :
-H "charset=utf-8"

how can i call web services with curl with json param?

I'm programming in Java.
I developed a test web service in which I pass a random json and the service returns me hello
Here the web service code:
#Path("test")
#GET
#Consumes(MediaType.APPLICATION_JSON)
public String test(#HeaderParam("token") String token, #QueryParam("array")String array)
{
return "hello";
}
I call the service with curl
curl -v -H 'token:aaaa' 'http://140.145.1.2/clayapi/restservices/test?array=[{"name":"george","lastname":"ronney"}]';
message of error:
curl: (3) [globbing] illegal character in range specification at pos 60
I tried to add -g but it doesn't work .. how should I do?
Use -G along with --data-urlencode:
curl -v -G 'http://example.org' \
-H 'header:value' \
--data-urlencode 'array=[{"name":"george","lastname":"ronney"}]'
From the documentation:
-G, --get
When used, this option will make all data specified with -d, --data, --data-binary or --data-urlencode to be used in an HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to the URL with a ? separator. [...]
--data-urlencode <data>
(HTTP) This posts data, similar to the other -d, --data options with the exception that this performs URL-encoding. [...]

Converting a Authenticated curl request to Apache Camel Request

curl -s -S -u user123:321pass https://12.15.13.12:3216 --data
'<?xml version="1.0" encoding="UTF-8"?><data>Hello Man</data>' -H 'Content-Type: text/xml' -k
I need to change this to a Apache Camel route call like :-
private ProducerTemplate producer;// there are setters for this.
producer.requestBodyAndHeaders(endpointUri, requestBody,addHeaders, String.class);
Main question is how do I pass username and password with this requestBodyAndHeaders.I tried passing through headers(header being map, key value pairs)

curl --data-urlencode UTF-8

I would like to send a HTTP GET request that contains a json as a parameter. The server expects the parameter to be encoded with UTF-8.
curl -G -v "http://localhost:8000/job" --data-urlencode "request={"clientId"="13","message"="I want apples"}"
I have used --data-urlencode but it doesn't seam to encode in UTF-8 because the server cannot decode the request.
I have a java client that uses
String charset = StandardCharsets.UTF_8.name();
String requestParameter = request={"clientId":"13","message"="I want apples"}
URLEncoder.encode(requestParameter, charset)
to encode the parameter and then send the HTTP GET request and the server accepts the request and is able to decode correctly.
The correct curl command is:
curl -G -v "http://localhost:8000/job" --data-urlencode 'request={"clientId":"13","messave":"I want apples"}'

Categories

Resources