Skip to content

Support Array Body Parameters in HTTP Methods #16

@elwafa

Description

@elwafa

Currently, the Step class's HTTP methods (POST, PUT, PATCH) only accept string body parameters. This feature request proposes changing these methods to accept array parameters instead, with automatic conversion to the appropriate string format based on the Content-Type header.

// Current approach requires manual conversion
$data = ['name' => 'John', 'age' => 30];
$step->header('Content-Type', 'application/json')
    ->post('http://example.com', json_encode($data));

The solution

Change the body parameter in HTTP methods to accept arrays instead of strings, with automatic conversion based on the Content-Type header:

// Proposed solution
$step->header('Content-Type', 'application/json')
    ->post('http://example.com', [
        'name' => 'John',
        'age' => 30
    ]);

// For form data
$step->header('Content-Type', 'application/x-www-form-urlencoded')
    ->post('http://example.com', [
        'username' => 'john_doe',
        'password' => 'secret'
    ]);

The conversion would be handled automatically:

  • application/json → JSON encoded string
  • application/x-www-form-urlencoded → URL-encoded query string
  • Default to JSON if no Content-Type is specified

Alternative

  1. Create separate methods for different content types (postJson, postForm, etc.)
  2. Accept both string and array parameters using union types
  3. Add a separate body formatter class/trait

Additional context

This would be a breaking change for existing code that passes string bodies directly. Implementation would require updates to:

  • src/Step.php
  • Associated test files
  • Documentation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions