Reputation: 2113
I'm performing a unit test for my API endpoints in Slim. This is what I used to pass data to the endpoint:
$requestData = [
'field1' => 123,
'field2' => 4567,
....
]
$request->withParsedBody($requestData);
Now need to test the endpoint with XML or JSON string like below -
$requestData = '<xml>
<appid><![CDATA[app123]]></appid>
<device_info><![CDATA[test-device-5678]]></device_info>
....
</xml>';
$request->withParsedBody($requestData);
The problem is that withParsedBody() only accepts array or object. So my question is: what's the proper way to pass raw data string to the request?
Upvotes: 0
Views: 2083
Reputation: 5
For me it would be better
$requestData = '<xml>
<appid><![CDATA[app123]]></appid>
<device_info><![CDATA[test-device-5678]]></device_info>
....
</xml>';
$streamFactory = new \Zend\Diactoros\StreamFactory();
$stream = $streamFactory->createStream($requestData);
$request->withBody($stream);
echo (string) $request->getBody();
Upvotes: 0
Reputation: 2113
This is the answer based on Dusan's comments.
$request->getBody()->write($requestData);
$request->reparseBody();
The reparseBody() call will force the request object to parse the new content again.
Upvotes: 1