Home > Enterprise >  Malformed UTF-8 error on PUT request with Dropwizard Jersey Client and Jackson
Malformed UTF-8 error on PUT request with Dropwizard Jersey Client and Jackson

Time:02-15

I have a Dropwizard application that queries an external REST API using a Jersey client and mapping the data with Jackson. GET requests work alright (including object mapping), but PUT resquests fail with error 500 and when I check the REST API server logs, it shows the following:

2022-02-09T11:00:34 [F|app|bd4d2aec]   
 bd4d2aec | JSON::GeneratorError (source sequence is illegal/malformed utf-8):
 bd4d2aec |   
 bd4d2aec | lib/server/middleware/catch_json_parse_errors.rb:16:in `rescue in call'
 bd4d2aec | lib/server/middleware/catch_json_parse_errors.rb:8:in `call'
 bd4d2aec | lib/server/middleware/logging_context_session.rb:22:in `call'
 bd4d2aec | lib/server/middleware/logging_context_request.rb:11:in `call'

The request body JSON code required is:

{"hostgroup": {"description": "Test"}}

I tested it with regular api clients and it works. This is how I translated it into Kotlin objects:

data class UpdateHostgroupInput(val hostgroup: UpdateDescriptionInput)
data class UpdateDescriptionInput(val description: String?)

And this is how I build my client and my request:

val client = JerseyClientBuilder(environment)
.using(configuration.APIServerConfiguration.client)
.using(environment.objectMapper).build("API-client")
.target(url)
.register(HttpAuthenticationFeature.basic(user, password))

val parameter = UpdateHostgroupInput(UpdateDescriptionInput("Test"))

client.path(endpoint).request(MediaType.APPLICATION_JSON_TYPE).put(Entity.json(parameter))

I tried to explicitly declare UTF-8 encoding, but it seems to be default for Jackson. I also tried forgetting about the objects and directly write JSON as a UTF-8 encoded string, but the result was the same. I've discarded the API itself being malfunctioning since I was able to perform the same request from Python successfuly.

Any hint on what I'm doing wrong here? Thanks for your help.

CodePudding user response:

I solved the issue by disabling gzip in the Jersey client from the config file (yml):

jerseyClient:
  gzipEnabled: false

I guess it's not compatible with the server I'm querying against.

  • Related