简体   繁体   中英

POSTing with Content-Type: multipart/form-data results in empty request body

Recently I started getting validation errors when POSTing to my REST interface with Content-Type: multipart/form-data . Content-Type: application/x-www-form-urlencoded is working.

Here is my testing function:

/**
 * @Rest\Route("/testtype")
 */
public function postTypeTestAction()
{
    $request = $this->getRequest()->request->all();
    $phpContents = file_get_contents("php://input");

    return FOSView::create()->setStatusCode(200)->setData(array('request'=>$request, 'phpinput' => $phpContents));
}

When I POST using Content-Type: application/x-www-form-urlencoded :

{
  "request":{
    "test":"message"
   },
  "phpinput":"test=message"
}

When I POST using Content-Type: multipart/form-data :

{
  "request":[
  ],
 "phpinput":"------WebKitFormBoundaryFyQqAxqqfuhWzHUq\r\nContent-Disposition: form-data; name=\"test\"\r\n\r\nmessage\r\n------WebKitFormBoundaryFyQqAxqqfuhWzHUq--\r\n"

}

Since there is no request data, I'm getting This value should not be blank validation errors. This breaks my application. I've been staring at this for so long, I'm certain I'm missing something simple.

I'm using Symfony 2.3.7 and FOSRestBundle 1.0.0.

The issue resolved itself literally overnight. No server reboot, no change to code, and I'm using the same tool to test ( DHC by Restlet - a Chrome extension). However, since this was not working on both dev and staging environments, and is now working on both environments, this leaves me with the only answer being the testing tool, Chrome, a local caching issue or some combination.

Lesson learned: use multiple testing tools.

Now when I POST using Content-Type: application/x-www-form-urlencoded :

{
  "request":{
    "test":"message"
   },
  "phpinput":"test=message"
}

Now when I POST using Content-Type: multipart/form-data :

{
  "request":{
    "test":"message"
   },
  "phpinput":""
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM