2222
2323use GuzzleHttp \Middleware ;
2424use Phing \Exception \BuildException ;
25+ use Phing \Project ;
2526use Phing \Type \Parameter ;
27+ use Phing \Type \Payload ;
2628use Phing \Util \Regexp ;
2729use Phing \Util \RegexpException ;
2830use Phing \Util \StringHelper ;
@@ -74,6 +76,16 @@ class HttpRequestTask extends HttpTask
7476 */
7577 private $ regexp ;
7678
79+ /**
80+ * The property where the http response is stored.
81+ */
82+ private string $ responseProperty = '' ;
83+
84+ /**
85+ * Payload to send as request body.
86+ */
87+ private ?Payload $ payload = null ;
88+
7789 /**
7890 * Sets the response regex
7991 *
@@ -94,6 +106,14 @@ public function setResponseCodeRegex($regex)
94106 $ this ->responseCodeRegex = $ regex ;
95107 }
96108
109+ /**
110+ * Set the name of the property where the HTTP response is stored.
111+ */
112+ public function setResponseProperty (string $ property ): void
113+ {
114+ $ this ->responseProperty = $ property ;
115+ }
116+
97117 /**
98118 * Sets whether to enable detailed logging
99119 *
@@ -126,6 +146,15 @@ public function createPostParameter()
126146 return $ this ->postParameters [$ num - 1 ];
127147 }
128148
149+ /**
150+ * Creates the body of the current request.
151+ */
152+ public function createPayload (): Payload
153+ {
154+ $ this ->payload = new Payload ();
155+ return $ this ->payload ;
156+ }
157+
129158 /**
130159 * Load the necessary environment for running this task.
131160 *
@@ -146,7 +175,16 @@ public function init()
146175 */
147176 protected function request ($ options = [])
148177 {
149- if ($ this ->method === 'POST ' ) {
178+ $ hasPostParameters = \count ($ this ->postParameters ) > 0 ;
179+ $ hasPayload = $ this ->payload instanceof Payload;
180+
181+ if ($ hasPostParameters && $ hasPayload ) {
182+ $ message = 'Cannot use <postparameter/> and <payload/> simultaneously. ' ;
183+ $ this ->log ($ message , Project::MSG_ERR );
184+ throw new BuildException ($ message );
185+ }
186+
187+ if ($ hasPostParameters && $ this ->method === 'POST ' ) {
150188 $ idx = ($ this ->isHeaderSet ('content-type ' , 'application/json ' ) ? 'json ' : 'form_params ' );
151189 $ options [$ idx ] = array_reduce (
152190 $ this ->postParameters ,
@@ -157,6 +195,12 @@ function ($carry, Parameter $postParameter) {
157195 );
158196 }
159197
198+ if ($ hasPayload ) {
199+ // Guzzle: the "body" option cannot be used with "form_params", "multipart", or "json".
200+ unset($ options ['form_params ' ], $ options ['multipart ' ], $ options ['json ' ]);
201+ $ options ['body ' ] = $ this ->payload ->getText ();
202+ }
203+
160204 if ($ this ->verbose ) {
161205 self ::getHandlerStack ()->push (Middleware::log (new ConsoleLogger (new ConsoleOutput ()), new \GuzzleHttp \MessageFormatter ()));
162206 }
@@ -205,5 +249,10 @@ protected function processResponse(ResponseInterface $response)
205249
206250 $ this ->log ('The response status-code matched the provided regex. ' );
207251 }
252+
253+ if ($ this ->responseProperty !== '' ) {
254+ $ this ->getProject ()->setNewProperty ($ this ->responseProperty , $ response ->getBody ());
255+ $ this ->log ("Saving response into ' {$ this ->responseProperty }' property. " );
256+ }
208257 }
209258}
0 commit comments