jackxujh
jackxujh

Reputation: 1023

Decode plus sign (+) to space in URLComponent

I have a query item in a URL: q=Yellowstone+national+park. When using URLComponents to decode and extract the query value using the following, I get Yellowstone+national+park instead of Yellowstone national park.

let someURL = URL(string: "https://www.example.com/?q=Yellowstone+national+park")!
let components = URLComponents(url: someURL, resolvingAgainstBaseURL: true)

let keyword = components?.queryItems?.first(where: { (queryItem) -> Bool in
        return queryItem.name == "q"
    })?.value ?? "Keyword is empty"

I found URLComponents work fine with percent-encoded queries:

q=%E8%BF%87%E5%B1%B1%E8%BD%A6 --> 过山车

q=Yellowstone+national+park --> Yellowstone+national+park

Is there anyway URLComponents can automatically convert + to space ? Thanks!

Upvotes: 3

Views: 2937

Answers (1)

maximus
maximus

Reputation: 746

from Apple Documentation:

RFC 3986 specifies which characters must be percent-encoded in the query component of a URL, but not how those characters should be interpreted. The use of delimited key-value pairs is a common convention, but isn't standardized by a specification. Therefore, you may encounter interoperability problems with other implementations that follow this convention.

One notable example of potential interoperability problems is how the plus sign (+) character is handled:

According to RFC 3986, the plus sign is a valid character within a query, and doesn't need to be percent-encoded. However, according to the W3C recommendations for URI addressing, the plus sign is reserved as shorthand notation for a space within a query string (for example, ?greeting=hello+world).

If a URL query component contains a date formatted according to RFC 3339 with a plus sign in the timezone offset (for example, 2013-12-31T14:00:00+00:00), interpreting the plus sign as a space results in an invalid time format. RFC 3339 specifies how dates should be formatted, but doesn't advise whether the plus sign must be percent-encoded in a URL. Depending on the implementation receiving this URL, you may need to preemptively percent-encode the plus sign character.

Don't know the reason for downvote the answer:

similar issue described here: Encode '+' using URLComponents in Swift

Upvotes: 4

Related Questions