CircleOfNice / CiRestClientBundle

Mapper for PHP internal curl library
GNU General Public License v3.0
57 stars 20 forks source link

Post request payload clarification #42

Closed zelding closed 7 years ago

zelding commented 7 years ago

Please clarify this for me: I'm using a POST request which has a query parameter also, but the payload doesn't seem to get through. (Both arrays are key-value pairs)

$restClient->post(
    $someApiUrl . "?" . http_build_query($someArray),
    http_build_query($someOtherArray)
);
Timo-Linde commented 7 years ago

please provide more informations about the returned response object

zelding commented 7 years ago

Here is the response var_dump:

object(Symfony\Component\HttpFoundation\Response)[2281]
      public 'headers' => 
        object(Symfony\Component\HttpFoundation\ResponseHeaderBag)[2282]
          protected 'computedCacheControl' => 
            array (size=1)
              'no-cache' => boolean true
          protected 'cookies' => 
            array (size=0)
              empty
          protected 'headerNames' => 
            array (size=8)
              'cache-control' => string 'Cache-Control' (length=13)
              'date' => string 'Date' (length=4)
              'server' => string 'Server' (length=6)
              'content-type' => string 'Content-Type' (length=12)
              'content-length' => string 'Content-Length' (length=14)
              'connection' => string 'Connection' (length=10)
              'via' => string 'Via' (length=3)
              'strict-transport-security' => string 'Strict-Transport-Security' (length=25)
          protected 'headers' => 
            array (size=8)
              'cache-control' => 
                array (size=1)
                  0 => string 'no-cache' (length=8)
              'date' => 
                array (size=1)
                  0 => string 'Fri, 04 Nov 2016 12:29:29 GMT' (length=29)
              'server' => 
                array (size=1)
                  0 => string 'nginx/1.6.2' (length=11)
              'content-type' => 
                array (size=1)
                  0 => string 'application/json' (length=16)
              'content-length' => 
                array (size=1)
                  0 => string '3' (length=1)
              'connection' => 
                array (size=1)
                  0 => string 'keep-alive' (length=10)
              'via' => 
                array (size=1)
                  0 => string '1.1 vegur' (length=9)
              'strict-transport-security' => 
                array (size=1)
                  0 => string 'max-age=500' (length=11)
          protected 'cacheControl' => 
            array (size=0)
              empty
      protected 'content' => string '{} ' (length=3)
      protected 'version' => string '1.0' (length=3)
      protected 'statusCode' => int 200
      protected 'statusText' => string 'OK' (length=2)
      protected 'charset' => null
Timo-Linde commented 7 years ago

maybe the destination api only accepts 'application/json' content-type

try:

$restClient->post(
    $someApiUrl . "?" . http_build_query($someArray),
    json_encode($someOtherArray)
);

if not: Try to provide a full bunch of informations. example $someArray example $someOtherArray example $someApiUrl Target Api authentication seems to be correct because the api answers with a HTTP_OK

zelding commented 7 years ago

I've tried it with raw curl

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($someOtherArray));

json payload didn't made a difference.

Examples:

    $someArray = ['token' => "a0b1c2d3e4f5"];
    $someotherArray = [
        'value1' => 'someString',
        'value2' => 'someOtherString',
        'value3' => 2.7,
        'value4' => 34
    ];
    $someApiUrl = 'https://host.net/api/v1/model/id/something';
Timo-Linde commented 7 years ago

Than it should work: CiRestClientBundle does nothing else

    /**
     * sets the payload
     *
     * @param  $payload
     * @return Curl
     */
    private function setPayload($payload) {
        $this->curlOptionsHandler->setOption(CURLOPT_POSTFIELDS, $payload);
        return $this;
    }

Maybe you forgot to set other options. Like this for json:

circle_rest_client:
    curl:
      defaults:
        CURLOPT_HTTPHEADER:     [ 'Content-Type: application/json' ]
zelding commented 7 years ago

That was it, thank you!

I left out the whole configuration, once i set
CURLOPT_HTTPHEADER: [ 'Content-Type: application/x-www-form-urlencoded' ] it was fine.

Timo-Linde commented 7 years ago

Nice. This can be closed .

passi246 commented 7 years ago

Thank you Timo for figuring that out. Closed.