Buy

Finish POST with a Form

To create a programmer, our client needs to send up some data. And while you can send that data as JSON, XML, form-encoded or any insane format you dream up, you'll probably want your clients to send JSON... unless you work for the banking or insurance industry. They love XML.

For the JSON, we can design it to have any keys. But since our Programmer entity has the properties nickname, avatarNumber and tagLine, let's use those.

These don't have to be the same, but it makes life easier if you can manage it.

Back in testing.php, create a $nickname - but make it a little bit random: this has a unique index in the database and I don't want everything to blow up if I run the file twice. Make a $data array and put everything in it. The avatarNumber is which built-in avatar you want - it's a number from 1 to 6. And add a tagLine:

25 lines testing.php
... lines 1 - 11
$nickname = 'ObjectOrienter'.rand(0, 999);
$data = array(
'nickname' => $nickname,
'avatarNumber' => 5,
'tagLine' => 'a test dev!'
);
... lines 18 - 25

To send this data, add an options array to post. It has a key called body, and it's literally the raw string you want to send. So we need to json_encode($data):

25 lines testing.php
... lines 1 - 11
$nickname = 'ObjectOrienter'.rand(0, 999);
$data = array(
'nickname' => $nickname,
'avatarNumber' => 5,
'tagLine' => 'a test dev!'
);
$response = $client->post('/api/programmers', [
'body' => json_encode($data)
]);
... lines 22 - 25

Reading the Request Body

This looks good - so let's move to our controller. To read the data the client is sending, we'll need the Request object. So add that as an argument:

24 lines src/AppBundle/Controller/Api/ProgrammerController.php
... lines 1 - 7
use Symfony\Component\HttpFoundation\Request;
... lines 9 - 12
/**
* @Route("/api/programmers")
* @Method("POST")
*/
public function newAction(Request $request)
{
... lines 19 - 21
}
... lines 23 - 24

To get the JSON string, say $body = $request->getContent(). And to prove things are working, just return the POST'd body right back in the response:

24 lines src/AppBundle/Controller/Api/ProgrammerController.php
... lines 1 - 16
public function newAction(Request $request)
{
$data = $request->getContent();
return new Response($data);
}
... lines 23 - 24

The client is sending a JSON string and our response is just sending that right back. Try it!

php testing.php

Hey, that's prefect! We get a 200 status code response and its content is the JSON we sent it. Time to pack it up and call it a day. Just kidding.

Create the Programmer

Now that we've got the JSON, creating a Programmer is ridiculously simple. First, json_decode the $body into an array:

33 lines src/AppBundle/Controller/Api/ProgrammerController.php
... lines 1 - 17
public function newAction(Request $request)
{
$data = json_decode($request->getContent(), true);
... lines 21 - 30
}
... lines 32 - 33

For now, we'll trust the JSON string has a valid structure. And the second argument to json_decode makes sure we get an array, not a stdClass object.

Now for the most obvious code you'll see $programmer = new Programmer(), and pass it $data['nickname'] and $data['avatarNumber'] - I gave this entity class a __construct() function with a few optional arguments. Now, $programmer->setTagLine($data['tagLine']):

33 lines src/AppBundle/Controller/Api/ProgrammerController.php
... lines 1 - 17
public function newAction(Request $request)
{
$data = json_decode($request->getContent(), true);
$programmer = new Programmer($data['nickname'], $data['avatarNumber']);
$programmer->setTagLine($data['tagLine']);
... lines 24 - 30
}
... lines 32 - 33

The only tricky part is that the Programmer has a relationship to the User that created it, and this is a required relationship. On the web, I'm logged in, so that controller sets my User object on this when I create a Programmer. But our API doesn't have any authentication yet - it's all anonymous.

We'll add authentication later. Right now, we need a workaround. Update the controller to extend BaseController - that's something I created right in AppBundle/Controller that just has some handy shortcut methods. This will let me say $programmer->setUser($this->findUserByUsername('weaverryan')):

33 lines src/AppBundle/Controller/Api/ProgrammerController.php
... lines 1 - 4
use AppBundle\Controller\BaseController;
... lines 6 - 11
class ProgrammerController extends BaseController
{
... lines 14 - 17
public function newAction(Request $request)
{
$data = json_decode($request->getContent(), true);
$programmer = new Programmer($data['nickname'], $data['avatarNumber']);
$programmer->setTagLine($data['tagLine']);
$programmer->setUser($this->findUserByUsername('weaverryan'));
... lines 25 - 30
}
}

So we're cheating big time... for now. At least while developing, that user exists because it's in our fixtures. I'm not proud of this, but I promise it'll get fixed later.

Finish things off by persisting and flushing the Programmer:

33 lines src/AppBundle/Controller/Api/ProgrammerController.php
... lines 1 - 17
public function newAction(Request $request)
{
... lines 20 - 23
$programmer->setUser($this->findUserByUsername('weaverryan'));
$em = $this->getDoctrine()->getManager();
$em->persist($programmer);
$em->flush();
... lines 29 - 30
}
... lines 32 - 33

Enjoy this easy stuff... while it lasts. For the Response, what should we return? Ah, let's worry about that later - return a reassuring message, like It worked. Believe me, I'm an API!:

33 lines src/AppBundle/Controller/Api/ProgrammerController.php
... lines 1 - 17
public function newAction(Request $request)
{
... lines 20 - 27
$em->flush();
return new Response('It worked. Believe me - I\'m an API');
}
... lines 32 - 33

The whole flow is there, so go back and hit the script again:

php testing.php

And... well, I think it looks like that probably worked. Now, let's add a form.

Leave a comment!

  • 2016-11-15 Caro

    yes it did, thank you so much!

  • 2016-11-14 weaverryan

    Hey Caro!

    I totally want you to be able to replicate this :). On this page, you should see a "Download" link in the upper right corner, with a "Course Code" option in it. This will download a .tar file. Inside, there is a "start" directory (how the code looks at the beginning of this tutorial) and a "finish" directory (how the code looks at the end of the tutorial).

    Does this help? Or is there still something missing?

    Cheers!

  • 2016-11-14 Caro

    For those of us who choose to purchase just this tutorial, I think providing the full picture (e.g. the Programmer model) would make this easier to replicate.

  • 2016-09-19 Victor Bocharsky

    Hey Greg,

    That's because Postman sends form-data/raw with a different headers. For the raw type "Content-Type" header equals to "text/plain" so the Symfony's Request object do not parse it itself, you need to call $request->getContent() to get request content. But for the form-data type "Content-Type" header of request is "multipart/form-data", so Request object parse content and you put data on the "$request" property for POST data and on the "$query" property for GET request. So if you send a POST request with "form-data" type via Postman - then you can get already parsed data in app with $request->request->get('your-sent-key-here').

    P.S. use "dump($request->request->all())" to dump all sent POST data in your app.

    Cheers!

  • 2016-09-18 Greg

    Hi,

    Thanks for you tutorail it are always amazing.
    I have a little question, I try to use Postman to make my call for "api/programmes" with in body form-data (nickname in key and Bob for value etc for the others data) I have nothing in $request->getContent()

    Edited:
    I found how to send data I need to use raw but why can I use form-data ?

    If I use Guzzle I have no pb.
    Do you know why?

    Thanks again.