Skip to content

Commit

Permalink
Added support for application/json to HttpRequestTask (phingofficial#720
Browse files Browse the repository at this point in the history
)
  • Loading branch information
siad007 authored and mrook committed May 24, 2017
1 parent 68ae377 commit a282fcb
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions classes/phing/tasks/ext/HttpRequestTask.php
Expand Up @@ -161,8 +161,12 @@ protected function createRequest()
if ($this->method == HTTP_Request2::METHOD_POST) {
$request->setMethod(HTTP_Request2::METHOD_POST);

foreach ($this->postParameters as $postParameter) {
$request->addPostParameter($postParameter->getName(), $postParameter->getValue());
if ($this->isHeaderSet('content-type', 'application/json')) {
$request->setBody(json_encode(array_map(function ($postParameter) {return [$postParameter->getName() => $postParameter->getValue()];}, $this->postParameters)));
} else {
foreach ($this->postParameters as $postParameter) {
$request->addPostParameter($postParameter->getName(), $postParameter->getValue());
}
}
}

Expand All @@ -178,6 +182,19 @@ protected function createRequest()
return $request;
}

private function isHeaderSet($headerName, $headerValue)
{
$isSet = false;

foreach ($this->headers as $header) {
if ($header->getName() === $headerName && $header->getValue() === $headerValue) {
$isSet = true;
}
}

return $isSet;
}

/**
* Checks whether response body matches the given regexp
*
Expand Down

0 comments on commit a282fcb

Please sign in to comment.