diff --git a/3.0/docs/url-encoded-form/getting-started.md b/3.0/docs/url-encoded-form/getting-started.md new file mode 100644 index 00000000..9c98833b --- /dev/null +++ b/3.0/docs/url-encoded-form/getting-started.md @@ -0,0 +1,42 @@ +# Getting Started with URL-Encoded Form + +URL-Encoded Form ([vapor/url-encoded-form](https://github.com/vapor/url-encoded-form)) is a small package that helps you parse and serialize `application/x-www-form-urlencoded` data. URL-encoded forms are a widely-supported encoding on the web. It's most often used for serializing web forms sent via POST requests. + +The URL-Encoded Form package makes it easy to use this encoding by integrating directly with `Codable`. + +## Vapor + +This package is included with Vapor and exported by default. You will have access to all `URLEncodedForm` APIs when you import `Vapor`. + +```swift +import Vapor +``` + +## Standalone + +The URL-Encoded Form package is lightweight, pure-Swift, and has very few dependencies. This means it can be used to work with `form-urlencoded` data for any Swift project—even one not using Vapor. + +To include it in your package, add the following to your `Package.swift` file. + +```swift +// swift-tools-version:4.0 +import PackageDescription + +let package = Package( + name: "Project", + dependencies: [ + ... + .package(url: "https://github.com/vapor/url-encoded-form.git", from: "1.0.0"), + ], + targets: [ + .target(name: "Project", dependencies: ["URLEncodedForm", ... ]) + ] +) +``` + +Use `import URLEncodedForm` to access the APIs. + +!!! warning + Some of this guide may contain Vapor-specific APIs, however most of it should be applicable to the URL-Encoded Form package in general. + Visit the [API Docs](https://api.vapor.codes/url-encoded-form/latest/URLEncodedForm/index.html) for specific API info. + diff --git a/3.0/docs/url-encoded-form/overview.md b/3.0/docs/url-encoded-form/overview.md new file mode 100644 index 00000000..e2ad8bfc --- /dev/null +++ b/3.0/docs/url-encoded-form/overview.md @@ -0,0 +1,126 @@ +# Using URL-Encoded Form + +URL-Encoded Form is a widely-supported encoding on the web. It's most often used for serializing web forms sent via POST requests. This encoding is also used to send structured data in URL query strings. + +It is a relatively efficient encoding for sending small amounts of data. However, all data must be percent-encoded making this encoding suboptimal for large amounts of data. See the [Multipart](../multipart/getting-started.md) encoding if you need to upload things like files. + +!!! tip + URL-Encoded Form integrates with [`Content`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Content.html) like all other encoding methods in Vapor. See [Vapor → Content](../vapor/content.md) for more information about the [`Content`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Content.html) protocol. + +Let's take a look at how to decode a `application/x-www-form-urlencoded` request. + +## Decode Body + +Most often, you will be decoding `form-urlencoded`-encoded requests from a web form. Let's take a look at what one of these requests might look like. After that, we will take a look at what the HTML form for that request would look like. + +### Request + +Here is an example `form-urlencoded`-encoded request for creating a new user. + +```http +POST /users HTTP/1.1 +Content-Type: application/x-www-form-urlencoded + +name=Vapor&age=3&luckyNumbers[]=5&luckyNumbers[]=7 +``` + +You can see the `[]` notation is used to encode arrays. Your web form will need to use this notation as well. + +### Form + +There are many ways to create a `form-urlencoded`-encoded request, but the most common is an HTML web form. Here is what the HTML form for this request might have looked like. + +```html +
+``` + +Since we are not specifying a special `enctype` attribute on the `