{"id":15372644,"url":"https://github.com/gmponos/http-message-util","last_synced_at":"2025-07-13T13:34:23.394Z","repository":{"id":62511420,"uuid":"162469449","full_name":"gmponos/http-message-util","owner":"gmponos","description":"A set of utilities for manipulating PSR-7 HTTP Requests/Responses","archived":false,"fork":false,"pushed_at":"2019-01-06T16:50:37.000Z","size":20,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-31T20:16:17.575Z","etag":null,"topics":["http","psr-7"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gmponos.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-19T17:25:01.000Z","updated_at":"2019-01-06T16:49:17.000Z","dependencies_parsed_at":"2022-11-02T13:02:08.666Z","dependency_job_id":null,"html_url":"https://github.com/gmponos/http-message-util","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmponos%2Fhttp-message-util","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmponos%2Fhttp-message-util/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmponos%2Fhttp-message-util/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmponos%2Fhttp-message-util/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gmponos","download_url":"https://codeload.github.com/gmponos/http-message-util/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239929748,"owners_count":19720196,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["http","psr-7"],"created_at":"2024-10-01T13:52:34.114Z","updated_at":"2025-02-20T23:25:38.884Z","avatar_url":"https://github.com/gmponos.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PSR-7 HTTP Message utilities\n\n[![Build Status](https://travis-ci.org/gmponos/http-message-util.svg?branch=master)](https://travis-ci.org/gmponos/http-message-util)\n[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/gmponos/monolog-slack/blob/master/LICENSE.md)\n\nThis package contains useful utility classes to manipulate PSR-7 Request/Response objects.\n\n**Important note:**\n\n- This package is still in version 0.x.x. According to [semantic versioning](https://semver.org/#spec-item-4) major changes can \noccur while we are still on 0.x.x version. If you use the package for a project that is in production please lock \nthis package in your composer to a specific version like ^0.3.0.\n\n## Description\n\nLet's say that you have a class that sends to an API a JSON body.\nMost probably you would have the following code:\n\n**Before** \n\n```php\n\u003c?php\nclass SendNotification\n{\n    private $client;\n    private $requestFactory;\n\n    public function __construct(ClientInterface $client, RequestFactoryInterface $requestFactory)\n    {\n        // PSR-17 factory\n        $this-\u003erequestFactory = $requestFactory;\n        // PSR-18 HTTP Client\n        $this-\u003eclient = $client;\n    }\n\n    public function send(string $email, string $message): \\Psr\\Http\\Message\\ResponseInterface\n    {\n        $request = $this-\u003erequestFactory-\u003ecreate('GET', 'http://www.testurl.com');\n        $body = $request-\u003egetBody();\n        if ($body-\u003eisSeekable() === false || $body-\u003eisWritable() === false) {\n            throw new \\InvalidArgumentException('Can not modify a request with non writable body.');\n        }\n\n        $content = json_encode(['email' =\u003e $email, 'message' =\u003e $message]);\n        if (JSON_ERROR_NONE !== json_last_error()) {\n            throw new \\InvalidArgumentException('Json encoding failed: ' . json_last_error());\n        }\n\n        $body-\u003ewrite($content);\n        $body-\u003erewind();\n        $request = $request-\u003ewithHeader('Content-Type', 'application/json');\n        return $this-\u003eclient-\u003esendRequest($request);\n    }\n}\n```\n\n**Using the utilities**\n\n```php\n\u003c?php\n\nuse HttpMessageUtil\\RequestUtil;\n\nclass SendNotification\n{\n    private $client;\n    private $requestFactory;\n\n    public function __construct(ClientInterface $client, RequestFactoryInterface $requestFactory)\n    {\n        // PSR-17 factory\n        $this-\u003erequestFactory = $requestFactory;\n        // PSR-18 HTTP Client\n        $this-\u003eclient = $client;\n    }\n\n    public function send(string $email, string $message): \\Psr\\Http\\Message\\ResponseInterface\n    {\n        $request = $this-\u003erequestFactory-\u003ecreate('GET', 'http://www.testurl.com');\n        $request = RequestUtil::withJsonBody($request, [\n            'email' =\u003e $email,\n            'message' =\u003e $message\n        ]);\n        return $this-\u003eclient-\u003esendRequest($request);\n    }\n}\n```\n\n## Install\n\nYou can install this package through composer\n\n```\n$ composer require gmponos/http-message-util\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n1. Run `composer install` from bash.\n2. Run `composer tests` from bash.\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmponos%2Fhttp-message-util","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgmponos%2Fhttp-message-util","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmponos%2Fhttp-message-util/lists"}