-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
notice: 'This file is imported and can be edited at https://github.com/amphp/http-server-form-parser/blob/2.x/README.md' | ||
title: http-server-form-parser | ||
description: 'Learn how to parse x-www-form-urlencoded or multipart/form-data data submitted to an HTTP server request handler.' | ||
image: undraw/undraw_server_cluster.svg | ||
permalink: /http-server-form-parser | ||
source: 'https://github.com/amphp/http-server-form-parser/blob/2.x/README.md' | ||
layout: docs | ||
--- | ||
This package is an add-on to [`amphp/http-server`](https://github.com/amphp/http-server), which allows parsing request bodies as forms in either `x-www-form-urlencoded` or `multipart/form-data` format. | ||
|
||
## Installation | ||
|
||
This package can be installed as a [Composer](https://getcomposer.org/) dependency. | ||
|
||
```bash | ||
composer require amphp/http-server-form-parser | ||
``` | ||
|
||
## Usage | ||
|
||
Basic usage works by calling `Form::fromRequest($request)`, which will buffer the request body and parse it. This method may be called multiple times, so both a [middleware](https://github.com/amphp/http-server#middleware) and [request handler](https://github.com/amphp/http-server#requesthandler) may access the form body. | ||
|
||
```php | ||
use Amp\Http\Server\FormParser\Form; | ||
use Amp\Http\Server\Request; | ||
use Amp\Http\Server\RequestHandler\ClosureRequestHandler; | ||
use Amp\Http\Server\Response; | ||
use Amp\Http\Status; | ||
|
||
$requestHandler = new ClosureRequestHandler(function (Request $request) { | ||
$form = Form::fromRequest($request); | ||
|
||
return new Response(Status::OK, [ | ||
"content-type" => "text/plain; charset=utf-8" | ||
], $form->getValue("text") ?? "Hello, World!"); | ||
}); | ||
``` | ||
|
||
There's also an advanced streaming parser included, `StreamingFormParser`, which can be used to stream uploaded files to disk or other locations. |