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

iSendARequestWithValues - encoding body? #21

Open
simplenotezy opened this issue Jan 13, 2015 · 4 comments
Open

iSendARequestWithValues - encoding body? #21

simplenotezy opened this issue Jan 13, 2015 · 4 comments

Comments

@simplenotezy
Copy link

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      |
@stof
Copy link
Member

stof commented Jan 13, 2015

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

@simplenotezy
Copy link
Author

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

@stof
Copy link
Member

stof commented Jan 13, 2015

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

@BrianGreenhill
Copy link

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();
    }

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

No branches or pull requests

3 participants