WebApiExtension icon indicating copy to clipboard operation
WebApiExtension copied to clipboard

iSendARequestWithValues - encoding body?

Open simplenotezy opened this issue 10 years ago • 4 comments

The following method:

    public function iSendARequestWithValues($method, $url, TableNode $post)
    {
        $url = $this->prepareUrl($url);
        $fields = array();

        foreach ($post->getRowsHash() as $key => $val) {
            $fields[$key] = $this->replacePlaceHolder($val);
        }

        $bodyOption = array(
          'body' => json_encode($fields),
        );
        $this->request = $this->client->createRequest($method, $url, $bodyOption);
        if (!empty($this->headers)) {
            $this->request->addHeaders($this->headers);
        }

        $this->sendRequest();
    }

Does not work, when the $fields is json encoded. When removing that line, it works perfectly. I.e, like this:

    public function iSendARequestWithValues($method, $url, TableNode $post)
    {
        $url = $this->prepareUrl($url);
        $fields = array();

        foreach ($post->getRowsHash() as $key => $val) {
            $fields[$key] = $this->replacePlaceHolder($val);
        }

        $bodyOption = array(
          'body' => $fields,
        );
        $this->request = $this->client->createRequest($method, $url, $bodyOption);
        if (!empty($this->headers)) {
            $this->request->addHeaders($this->headers);
        }

        $this->sendRequest();
    }

Now, before I submit a pull request, I want to make sure I am not getting something wrong? The bug seems too obvious.

I am testing with this data:

        When I send a POST request to "/items" with values:
            | attributes    | {"size":"1", "size_length":5} |
            | category      | 1      |

simplenotezy avatar Jan 13 '15 16:01 simplenotezy

The Gherkin step is not meant to contain a json string inside the table

stof avatar Jan 13 '15 16:01 stof

Well, how could I accomplish what I am trying to do? To send a POST request with some fields.

simplenotezy avatar Jan 13 '15 17:01 simplenotezy

This extension is meant to submit a request in JSON format, not in form-url-encoded format

stof avatar Jan 13 '15 17:01 stof

In my case, the POST data was not being received by my API. When I changed the function to read as follows, my issue was solved.

    /**
     * Sends HTTP request to specific URL with field values from Table.
     *
     * @param string    $method request method
     * @param string    $url    relative url
     * @param TableNode $post   table of post values
     *
     * @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)" with values:$/
     */
    public function iSendARequestWithValues($method, $url, TableNode $post)
    {
        $url = $this->prepareUrl($url);

        $data = ['query' => $post->getRowsHash()];

        $this->request = $this->getClient()->createRequest($method, $url, $data);

        $this->sendRequest();
    }

BrianGreenhill avatar Sep 02 '15 13:09 BrianGreenhill