Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added isJson and isXML functions to Response class #455

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from

Conversation

mstfblci
Copy link

Two new functions have been added to check if the response return is json or xml

@SRWieZ
Copy link

SRWieZ commented Sep 17, 2024

This could be really useful; in a lot of my Connectors, I do things like this:

public function hasRequestFailed(Response $response): ?bool
{
    // if psr response is json
    $contentType = $response->getPsrResponse()->getHeader('Content-Type');

    if (in_array('application/json', $contentType)) {
        return ! empty($response->json('errors'));
    }

    return null;
}

This PR would simplify it like this:

public function hasRequestFailed(Response $response): ?bool
{
    if ($response->isJson()) {
        return ! empty($response->json('errors'));
    }

    return null;
}

I would argue, though, that I prefer a more strict comparison than str_contains() proposed in this PR.

@mstfblci
Copy link
Author

@SRWieZ Thank you for your support.

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.

2 participants