2

I am trying to create a link that opens the New issue page on Github filling it with existing knowledge of the problem.

In order to do so, i am using the query parameters like followed:

https://github.com/User/Repository/issues/new?title=Some text&body=More Text

That works fine, however i am trying to format the document using Markdown and all symbols are being escaped after creating a new URL by calling

URL url = new URL("https://github.com/User/Repository/issues/new?title=Some text&body=# Header # Another header");

The result will be this:

https://github.com/User/Repository/issues/new?title=Some text&body=# Header %23 Another header

the second # is being escaped, but the first isn't and i don't quite understand why.

Any ideas?

Marcel
  • 1,509
  • 1
  • 17
  • 39

1 Answers1

3

In short, the URL parser is treating your first # as a fragment (a.k.a. anchor, e.g. <a name="named-anchor">). Since according to RFC-3986: Section 3, the fragment must come last and # is a reserved character, anything after that first # is assumed to be part of that fragment, causing the parser to encode any further "invalid" characters, such as your second #. From the RFC:

The generic URI syntax consists of a hierarchical sequence of components referred to as the scheme, authority, path, query, and fragment.

URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]

Note that fragment comes last, and is delimited by the #.

The best way to handle this would be to:

  1. encode the body query parameter on your own or
  2. use an HTTP client that does the escaping for you, e.g. RestTemplate from Spring or Apache HttpComponents.
Community
  • 1
  • 1
Brian
  • 17,079
  • 6
  • 43
  • 66
  • the problem is, that i dont do the http request myself, but use `Desktop.browse` instead, since i want the browser to open up. – Marcel Mar 06 '18 at 18:42
  • Then you want (1). Where you're creating your URL to pass in to `Desktop.browse`, use the answers from the link in my answer to encode your query parameters when creating it. – Brian Mar 06 '18 at 18:45