mollie / mollie-api-php

Mollie API client for PHP
http://www.mollie.com
BSD 2-Clause "Simplified" License
552 stars 191 forks source link

JSON request body does not represent an object #237

Closed KDederichs closed 6 years ago

KDederichs commented 6 years ago

Specifications

Describe the issue

Hey,

I'm using your API in combination with Symfony 4 and so far it was working fine. Out of the blue I started to get Error executing API call (400: Bad Request): The JSON request body does not represent an object..

Errors while trying to create new customers.

Any idea what could be causing this?

Smitsel commented 6 years ago

Can you post a piece of code to give some more context on what you're using?

KDederichs commented 6 years ago

Sure, the scenario is simple: I have a doctrine listener that queries the Mollie Api after a new customer is registered to get an ID from you. That is done simply by calling:

    /** @var MollieApiClient */
    private $mollie;

    public function __construct(MollieApiClient $mollie)
    {
        $this->mollie = $mollie;
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        /** @var Customer $customer */
        $customer = $args->getEntity();

        if (!$customer instanceof Customer) {
            return;
        }

        $em = $args->getEntityManager();

       $response = $this->mollie->customers->create();

        if ($response) {
            $customer->setMollieId($response->id);
            $em->persist($customer);
            $em->flush();
        }
    }

The MollieApiClient is registered as a service and is inject by DI into the listener. The MollieApiClient is simply registered by setting:


    GuzzleHttp\Client:
      alias: eight_points_guzzle.client.defaultClient

    Mollie\Api\MollieApiClient:
        calls:
            - method: setApiKey
              arguments:
                  - '%env(MOLLIE_API_KEY)%'
Smitsel commented 6 years ago

Hi @KDederichs,

The error is because you are not supplying any parameters to $this->mollie->customers->create(). Which tries to json_encode the parameters to send to our endpoint. Which will result into an error. Since we're actually trying to json_encode null.

This error is not really clear though.

KDederichs commented 6 years ago

It's actually encoding "[]" as httpBody though. The same code did work at one point as well, it just stopped somehow.

Smitsel commented 6 years ago

Yes sorry, it is actually a message given by the API. But the cause is the same, it needs parameters.

I'll check if we can alter this message in the API.

KDederichs commented 6 years ago

In that case your documentation is wrong though, cause all parameters in create customer are marked optional. So it would make sense that it works with no parameters: https://docs.mollie.com/reference/v2/customers-api/create-customer

willemstuursma commented 6 years ago

@smitsel This must be altered in the client. It should not send a body if there are no parameters (empty array).

Smitsel commented 6 years ago

Thanks for reporting this. The body should indeed not be passed when left empty.

I've committed and released the fix under https://github.com/mollie/mollie-api-php/releases/tag/v2.0.10