---
title: "query(percentEncoded:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/url/query(percentencoded:)"
---

# query(percentEncoded:)

Returns the query component of the URL, optionally removing any percent-encoding.

## Declaration

```swift
func query(percentEncoded: Bool = true) -> String?
```

## Parameters

- `percentEncoded`: A Boolean value that indicates whether the URL percent-encodes any unreserved characters. Defaults to true.

## Return Value

Return Value The query component of the URL, optionally percent-encoding any unreserved characters.

## Discussion

Discussion The system doesn’t allow certain characters in the URL query component, so URL percent-encodes those characters to create a valid URL. Calling this function with percentEncoded = false removes any percent-encoding and returns the unencoded query. If the URL doesn’t contain a query component according to RFC 3986, this function returns nil.

## See Also

### Accessing the parts of a URL

- [fragment(percentEncoded:)](foundation/url/fragment(percentencoded:).md)
- [fragment](foundation/url/fragment.md)
- [host(percentEncoded:)](foundation/url/host(percentencoded:).md)
- [host](foundation/url/host.md)
- [lastPathComponent](foundation/url/lastpathcomponent.md)
- [path(percentEncoded:)](foundation/url/path(percentencoded:).md)
- [path](foundation/url/path.md)
- [password(percentEncoded:)](foundation/url/password(percentencoded:).md)
- [password](foundation/url/password.md)
- [pathComponents](foundation/url/pathcomponents.md)
- [pathExtension](foundation/url/pathextension.md)
- [port](foundation/url/port.md)
- [query](foundation/url/query.md)
- [scheme](foundation/url/scheme.md)
- [user(percentEncoded:)](foundation/url/user(percentencoded:).md)
