Question

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.

Was it helpful?

Solution

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":""
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top