Strange URL Encoding Problem

I have a weird problem with urlencoding a plus sign +as a request parameter for an API request. The API documentation states:

The date must be in W3C format, for example .. '2016-10-24T13: 33: 23 + 02: 00'

So far, so good, so I use this code (minimized) to generate the URL using Spring UriComponentBuilder:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX");
ZonedDateTime dateTime = ZonedDateTime.now().minusDays(1);
String formated = dateTime.format(formatter);

UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUrl);
uriComponentsBuilder.queryParam("update", formated);
uriComponentsBuilder.build();
String url = uriComponentsBuilder.toUriString();

An uncoded request will look like this:

https://example.com?update=2017-01-05T12:40:44+01

The encoded string results in:

https://example.com?update=2017-01-05T12:40:44%2B01

which is (IMHO) a correctly encoded String request. See the %2Breplacement +at +01the end of the query string.

Now, when I send a request against the API using an encoded URL, I get an error when the request cannot be processed.

, , %2B + , :

url.replaceAll("%2B", "+");

, + whitespace. , URL-, ,

https://example.com?update=2017-01-05T12:40:44 01
  • ?

  • -, , API, , ?

UPDATE:

RFC 3986 ( 3.4) + .

3.4.

, ( 3.3), URI
( ).
mark ( "?" ) ( "#" )
URI.

Berners-Lee, et al. [ 23] RFC 3986 URI
2005 .

  query       = *( pchar / "/" / "?" )

( "/" ) ( "?" ) . , , , URI ( 5.1), , , . ,
" = " - URI, . .

qaru.site/questions/378614/..., Spring UriComponentBuilder , , , . , : UriComponentBuilder ?

+4
2

, , spring UriComponentBuilder URL-, false build() , toUriString() URL-, encode() build():

/**
 * Build a URI String. This is a shortcut method which combines calls
 * to {@link #build()}, then {@link UriComponents#encode()} and finally
 * {@link UriComponents#toUriString()}.
 * @since 4.1
 * @see UriComponents#toUriString()
 */
public String toUriString() {
    return build(false).encode().toUriString();
}

( ) - , . ( ) URI

String url = uriComponentsBuilder.build().toUri().toString(); // returns the unencoded url as a string
0

2017-01-05T12:40:44+01

2017-01-05T12%3A40%3A44%2B01

2017-01-05T12:40:44%2B01, .

, , .

0

Source: https://habr.com/ru/post/1665989/


All Articles