How to fix Unsupported Encoding "UTF-8" error?

Not applicable

Wondering if anyone knows how to fix Unsupported Encoding "UTF-8" error?

{"fault":{"detail":{"errorcode":"protocol.http.UnsupportedEncoding"},"faultstring":"Unsupported Encoding \"UTF-8\""}}

This error can be seen through trace session within API Management, just before the request is sent to the target server. The resource on the REST service is encoding the response as UTF-8. The last policy to be acted upon, before the error is thrown, is an AssignMessage policy type designating the target server url among other headers set.

Solved Solved
0 10 31.5K
1 ACCEPTED SOLUTION

Not applicable

Here is a snip of the JAX-RS service provider code:

@GET
@Path(value = "one/value")
@Produces("application/json")
@ApiOperation(value = "Returns a value", response = Object.class, responseContainer = "Response")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"),
		@ApiResponse(code = 500, message = "Internal error"), })
public Response getValue() {
	JsonObject value=null;
	try{
		value = Json.createObjectBuilder();
	}
	catch (Exception e){
		logger.error("Error" , e);	
		return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Error").build();
	}
	return Response
            .status(200)
            .entity(value.toString())
            .encoding("UTF-8")
            .lastModified(new Date())
            .build();
}

Solution: Remove UTF-8 encoding from the service provider response.

View solution in original post

10 REPLIES 10

Not applicable

Hello @Beal,

Would it be possible for you to share the Policy and the Request Trace session ?

It might seem silly, but the correct encoding name is "utf-8" (lower case). Do you have the option to change the uppercase to lowercase?

Not applicable

Sorry BASU, I cannot share the request trace on here. Paul I did try and send the encoding back as lowercase utf-8, but got the same error back, just in lowercase utf-8.

@Beal , Can you attach a sample proxy to reproduce above issue ? Any pointers to reproduce above issue will be helpful to find a resolution. Keep us posted.

Here is a sample policy within my proxy I have been working with:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Set-Target">
    <DisplayName>Set-Target</DisplayName>
    <Properties/>
    <Add>
        <Headers>
            <Header name="Target-URL">someHTTPendpoint/one/value</Header>
            <!--<Header name="Content-Type">charset=UTF-8</Header>-->
        </Headers>
    </Add>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

Here is a sample of the JAX-RS service provider resource:

@GET
@Path(value = "one/value")
@Produces("application/json")
@ApiOperation(value = "Returns a value", response = Object.class, responseContainer = "Response")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"),
		@ApiResponse(code = 500, message = "Internal error"), })
public Response getValue() {
	JsonObject value=null;
	try{
		value = Json.createObjectBuilder();
	}
	
	catch (Exception e){
		logger.error("Error" , e);	
		return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Error").build();
	}
	return Response.status(200).entity(value.toString()).encoding("UTF-8").lastModified(new Date()).build();
}

Looks like the issue has been .encoding("UTF-8") in the service response. Apigee would not accept an incoming request to my Apigee proxy without Content-Type="charset=UTF-8" being set. The problem with that is the service would return a 400 Bad Request error, if the above Content-Type was sent from the proxy to the service provider. The easiest solution to this problem has been to remove the UTF-8 encoding from the service resource.

Not applicable

Here is a snip of the JAX-RS service provider code:

@GET
@Path(value = "one/value")
@Produces("application/json")
@ApiOperation(value = "Returns a value", response = Object.class, responseContainer = "Response")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"),
		@ApiResponse(code = 500, message = "Internal error"), })
public Response getValue() {
	JsonObject value=null;
	try{
		value = Json.createObjectBuilder();
	}
	catch (Exception e){
		logger.error("Error" , e);	
		return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Error").build();
	}
	return Response
            .status(200)
            .entity(value.toString())
            .encoding("UTF-8")
            .lastModified(new Date())
            .build();
}

Solution: Remove UTF-8 encoding from the service provider response.

I am facing the exact same issue. Did you find a work around for this. I don't have the luxury to change the provider response. Please share if there is any possible solution.

Hi Mahana3,

Did you get it fixed?

Nope, Apigee Edge will throw a fault of the response from the backend is not valid according to the HTTP specification.

yes, per the reference doc on Response.ResponseBuilder, the .encoding() method sets the Content-Encoding header in the response.

And UTF-8 is not a valid value for that header.

See here

and here.