3

Problem:

No data and files are coming through to the Silex application when a request is made from codeception test using the REST module with PhpBrowser driver.

    // ApiTester $I
    $I->wantTo('Submit files');

    // prepare:
    $data = ['key' => 'value'];
    $files = [
        'file_key' => 'path/to/file.doc',
        'file2_key' => 'path/to/file2.doc'
    ];

    // act:
    $I->haveHttpHeader('Content-Type', 'multipart/form-data');
    $I->sendPOST('/attachments/', $data, $files);

Current response

 I have http header "Content-Type","multipart/form-data"
 I send post "/attachments/",{"key":"value"},{"file_key":"/path/to/file/...}
  [Request] POST http://localhost/attachments/ {"key":"value"}
  [Request Headers] {"Content-Type":"multipart/form-data"}
  [Page] http://localhost/attachments/
  [Response] 400
  [Request Cookies] []
  [Response Headers] {"Date":["Tue, 25 Oct 2016 09:15:31 GMT"],"Server":["Apache/2.4.10 (Debian)"],"Cache-Control":["no-cache"],"Access-Control-Allow-Origin":["*"],"Access-Control-Allow-Headers":["Content-Type, Authorization"],"Access-Control-Allow-Methods":["GET,PATCH,PUT,POST,HEAD,DELETE,OPTIONS"],"Content-Length":["1235"],"Connection":["close"],"Content-Type":["application/json"]}
  [Response] {"status":400,"meta":{"time":"2016-10-25 09:15:31"},"title":"Invalid Request","errors":"No data received","details":{"error_class":"Symfony\Component\HttpKernel\Exception\BadRequestHttpException"

Tried:

  • changing the Content-Type header
  • changing files array passed to sendPOST to an array of:
    • file paths file objects ( UploadedFile )
    • file arrays

The test works with Silex driver, but that won't be an option on the CI server. Also we've checked with Postman and the API route works as intended, files are sent and all good.

5
  • 1
    Have you tried to make a request without setting Content-Type? Commented Oct 25, 2016 at 16:57
  • tried, but then I get [Request Headers] {"Content-Type":"application/vnd.api+json"} and the API can't handle the request and get "Invalid Request", invalid data Commented Oct 26, 2016 at 12:15
  • 1
    the headers was the issue, it was set from a previous test, and $I->deleteHeader('content-type'); fixed it Commented Oct 26, 2016 at 13:42
  • should add this as an answer to get some more points :) Commented Oct 26, 2016 at 14:06
  • I don't know the root cause and can't come up with a good complete answer for it. Commented Oct 26, 2016 at 21:36

1 Answer 1

5

The actual problems:

  • $I->haveHttpHeader('Content-Type', 'multipart/form-data'); overwrites the Content-Type as it should be set by the http library (in phpbrowser is guzzle) to include the boundary, it's related to this.
  • Also be mindful that the $I->header does not reset after each request, to unset it use $I->deleteHeader('Content-Type');

Solution

  • Don't set the 'Content-Type' headers when sending files.
Sign up to request clarification or add additional context in comments.

2 Comments

Had the same issue and I wasn't setting Content-Type to multipart/form-data. And still I was getting this issue. However, $I->deleteHeader('Content-Type'); helped. Thank you
Something, somewhere probably did set it for the $I object before you made your request, as it should be empty by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.