dtb81443 2017-02-21 16:39
浏览 33

Slim - 将原始数据字符串传递给请求正文

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?

  • 写回答

1条回答 默认 最新

  • duandian4501 2017-02-22 12:48
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料