crnk-project / crnk-framework

JSON API library for Java
Apache License 2.0
287 stars 156 forks source link

Problem with encoding URL #750

Open uukeshov opened 4 years ago

uukeshov commented 4 years ago

I greet you!

I implemented a service that acts as a client for another REST service that is implemented according to the json api specification.

for my part, all models are created and their properties are described.

I had a problem with the character encoding [] when working with the service with the json API specification. the problem is that the service requires to encode these characters in the form (% 5B,% 5D%), respectively. Example:

Regular request: http://127.0.0.1:8080/api/subscriptions?filter[msisdn]=99999999

Required request type: http://127.0.0.1:8080/api/subscriptions?filter%5Bmsisdn%5D=99999999

how can this be done, can you tell?

@Bean
CrnkClient getCrnkClient() {
    CrnkClient crnkClient = new CrnkClient("http://someIp:port/api/v1");
    return crnkClient;
}

@SneakyThrows @Override public ResourceList findAll(QuerySpec querySpec) { ResourceList subscriptions = new DefaultResourceList<>(); ResourceRepository<Subscriptions, String> subscriptionsResourceRepositoryV2 = crnkClient.getRepositoryForType(Subscriptions.class); try { subscriptions = subscriptionsResourceRepositoryV2.findAll(querySpec); } catch (HttpServerErrorException e) { throw new HttpServerErrorException(HttpStatus.SERVICE_UNAVAILABLE, e.getMessage()); } catch (Exception e) { e.printStackTrace(); } return subscriptions; }

remmeier commented 4 years ago

what server are you using? what error message do you get? The use of [ and ] within URLs has a bit of history. One the spec side some people would like to have it escaped. On the other side none of the browsers actually do that. There is, for example, CrnkTomcatAutoConfiguration which enables support for it in Tomcat (on the server-side).

uukeshov commented 4 years ago

what server are you using?

for now i found workaround i'm intercepting HttpRequest and replacing this chracters [] to needed one.

Thank u, for reply!

chrylis commented 1 year ago

Tomcat 9 requires square brackets to be encoded and will return a 400 if they are not:

Invalid character found in the request target [&#47;v0&#47;resourceName?filter[someAttr]=matchValue ]. The valid characters are defined in RFC 7230 and RFC 3986

I'm using Crnk Client to consume an API published on Tomcat (not using Crnk), and this behavior produces OOTB failures.