I prefer to use GuzzleHttp for external requests:
use GuzzleHttp\Client; $client = new Client(); $response = $client->post($url, [ GuzzleHttp\RequestOptions::JSON => ['title' => 'title', 'body' => 'body'] ]);
Note: GuzzleHttp must be an installer version, for example. using composer.
But you can always use the client bundled with Symfony :
public function testJsonPostPageAction() { $this->client = static::createClient(); $this->client->request( 'POST', '/api/v1/pages.json', array(), array(), array('CONTENT_TYPE' => 'application/json'), '[{"title":"title1","body":"body1"},{"title":"title2","body":"body2"}]' ); $this->assertJsonResponse($this->client->getResponse(), 201, false); } protected function assertJsonResponse($response, $statusCode = 200) { $this->assertEquals( $statusCode, $response->getStatusCode(), $response->getContent() ); $this->assertTrue( $response->headers->contains('Content-Type', 'application/json'), $response->headers ); }
source share