3

I install Symfony 4 and then API Platform.

Then I create Test class like this

class UserFunctionalTest extends WebTestCase { /** @var string */ protected $host = "https://wikaunting-api.local"; /** @var KernelBrowser */ protected $client; protected function setUp() { $this->client = static::createClient(); } public function testCreateUser() { $response = $this->client->request('POST', $this->host . '/api/users.json', [ 'json' => [ 'username' => 'jamielann1', 'email' => '[email protected]', 'password' => 'jamielann1', ], ]); $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); } } 

When I run ./bin/phpunit, I got error message

Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException: "The content-type "application/x-www-form-urlencoded" is not supported. Supported MIME types are "application/ld+json", "application/json", "text/html"." at /home/vagrant/Code/testcode/vendor/api-platform/core/src/EventListener/DeserializeListener.php line 130 

My question is, why it is not received as application/json? What is the proper way?

4
  • May refer to: stackoverflow.com/questions/41713684/… Commented Sep 11, 2019 at 8:53
  • I see. I try to use what currently available, but if all doesn't work, I'll try Guzzle. Commented Sep 11, 2019 at 9:12
  • 1
    Hi @Permana have you checked also the second part of the suggested answer about the client bundled with Symfony ? Commented Sep 11, 2019 at 9:21
  • Hi @Matteo, I just tried using the second part. Yep, and it works. The request now recognized as application/json. Thank you for mentioning the second part. Commented Sep 12, 2019 at 0:46

2 Answers 2

3

From https://symfony.com/doc/current/testing.html#working-with-the-test-client

// submits a raw JSON string in the request body $client->request( 'POST', '/submit', [], [], ['CONTENT_TYPE' => 'application/json'], '{"name":"Fabien"}' ); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this worked for me :)
0

you can set the Content-Type header and set it to one of the json types (see your error message), the key to put the headers in, is headers

 $response = $this->client->request('POST', $this->host . '/api/users.json', [ 'json' => [ 'username' => 'jamielann1', 'email' => '[email protected]', 'password' => 'jamielann1', ], 'headers' => [ 'Content-Type' => 'application/json', // or just 'Content-Type: application/json', without the key ], ]); 

sadly the Symfony\Contracts\HttpClient\HttpClientInterface says something to the json parameter:

'json' => null, // array|\JsonSerializable - when set, implementations MUST set the "body" option to // the JSON-encoded value and set the "content-type" headers to a JSON-compatible // value if they are not defined - typically "application/json" 

which apparently doesn't work as expected ...

3 Comments

I tried to explicitly add the headers application/json, but still return the same result.
can you check the var/log/test.log if it possibly contains the request and see if it maybe is not authenticated or something?
I'm using Homestead, so I check /var/log/nginx/MYHOSTNAME.log and it only contain error for past event. I now can make it work using stackoverflow.com/questions/41713684/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.