WebApiExtension
WebApiExtension copied to clipboard
iSendARequestWithValues - encoding body?
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 |
The Gherkin step is not meant to contain a json string inside the table
Well, how could I accomplish what I am trying to do? To send a POST request with some fields.
This extension is meant to submit a request in JSON format, not in form-url-encoded format
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();
}