Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gmponos/http-message-util

A set of utilities for manipulating PSR-7 HTTP Requests/Responses
https://github.com/gmponos/http-message-util

http psr-7

Last synced: about 1 month ago
JSON representation

A set of utilities for manipulating PSR-7 HTTP Requests/Responses

Awesome Lists containing this project

README

        

# PSR-7 HTTP Message utilities

[![Build Status](https://travis-ci.org/gmponos/http-message-util.svg?branch=master)](https://travis-ci.org/gmponos/http-message-util)
[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/gmponos/monolog-slack/blob/master/LICENSE.md)

This package contains useful utility classes to manipulate PSR-7 Request/Response objects.

**Important note:**

- This package is still in version 0.x.x. According to [semantic versioning](https://semver.org/#spec-item-4) major changes can
occur while we are still on 0.x.x version. If you use the package for a project that is in production please lock
this package in your composer to a specific version like ^0.3.0.

## Description

Let's say that you have a class that sends to an API a JSON body.
Most probably you would have the following code:

**Before**

```php
requestFactory = $requestFactory;
// PSR-18 HTTP Client
$this->client = $client;
}

public function send(string $email, string $message): \Psr\Http\Message\ResponseInterface
{
$request = $this->requestFactory->create('GET', 'http://www.testurl.com');
$body = $request->getBody();
if ($body->isSeekable() === false || $body->isWritable() === false) {
throw new \InvalidArgumentException('Can not modify a request with non writable body.');
}

$content = json_encode(['email' => $email, 'message' => $message]);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException('Json encoding failed: ' . json_last_error());
}

$body->write($content);
$body->rewind();
$request = $request->withHeader('Content-Type', 'application/json');
return $this->client->sendRequest($request);
}
}
```

**Using the utilities**

```php
requestFactory = $requestFactory;
// PSR-18 HTTP Client
$this->client = $client;
}

public function send(string $email, string $message): \Psr\Http\Message\ResponseInterface
{
$request = $this->requestFactory->create('GET', 'http://www.testurl.com');
$request = RequestUtil::withJsonBody($request, [
'email' => $email,
'message' => $message
]);
return $this->client->sendRequest($request);
}
}
```

## Install

You can install this package through composer

```
$ composer require gmponos/http-message-util
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## Testing

1. Run `composer install` from bash.
2. Run `composer tests` from bash.

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.