Skip to content

Conversation

@elliota43
Copy link

Problem

When expectsJson() is called before body() with a different mime type, the expected_type gets overwritten, causing incorrect response parsing.

Example:

$response = \Httpful\Request::post($url)
    ->expectsJson()
    ->body(['abc'=>123], \Httpful\Mime::FORM)
    ->send();

This would incorrectly parse the JSON response as form data, resulting in:

Array (size=1) '{"exampleResponseFromServer": "data processed successfully"}' => string '' (length=e request body's content type and doesn't overwrite the expected response type.

Solution

Changed body() method to use contentType() instead of mime(), so it only sets the request body's content type and doesn't overwrite the expected response type.

This change:

  • Preserves expected_type when set before body()
  • Only sets content_type for the request body
  • Maintains backward compatibility (all existing tests pass)

Testing

Added test cases to verify:

  • expectsJson() before body() preserves expected_type
  • Response is correctly parsed as JSON
  • body() only sets content_type, not expected_type

All 46 tests pass (44 existing + 2 new).

Fixes #299

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

calling expectsJson() before body() of a post request breaks response parsing

2 participants