How to use REST to validate XML receiving via POST

Asked

Viewed 309 times

2

I have an application in Delphi that sends an XML with a code via Post. I have to develop an application in Symfony that needs to validate the receipt of this XML via post. To hear it would be via Rest but I never used Rest.

I would like an orientation for Symfony 3 and preferably in English.

2 answers

3

My dear, in this case you don’t have a cake recipe, the ideal is that you go to the Symfony documentation (https://symfony.com/doc/current/index.html) and take a look at what the framework makes available to you.

As in your case is a REST API, I strongly recommend using the Symfony-based micro-framework called Silex (http://silex.sensiolabs.org/). Because it is a micro-framework, it is much leaner, bringing the essentials to build a Rest API with a powerful stack.

Note: The links are in English, but there is no escape from it. Good luck!

2


Your question is quite broad, so I’ll explain "on top" what you can do if the idea is to actually use Symfony 3:

  1. create your Symfony 3 project with the command composer create-project symfony/framework-standard-edition <nome-do-projeto>; this will create a project with several basic dependencies for most web projects (Doctrine, Swiftmailer, Twig etc);
  2. create a class AppBundle\Entity\Data with the data you will receive (I put an example below);
  3. modify the only route to receive the request data and map it to the class created above.
  4. Enable the service serializer in his config.yml (example below);
  5. run your project with the command bin/console server:start
  6. test the route with a request POST by means of the Httpie: echo '<data><tag>oi</tag></data>' | http 127.0.0.1:8000/

Filing cabinet config.yml:

framework:
    serializer:
        enabled: true

(ps: no need to delete all key settings framework, just add the configuration of serializer).

Class AppBundle\Controller\DefaultController:

<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Data;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\SerializerInterface;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        $data = $this->getSerializer()->deserialize($request->getContent(), Data::class, 'xml');
        print_r($data); die;
    }

    /**
     * @return SerializerInterface
     */
    private function getSerializer()
    {
        return $this->get('serializer');
    }
}

Class AppBundle\Entity\Data:

<?php

namespace AppBundle\Entity;

class Data
{
    public $tag;
}

If all went well, you will have an instance of the class AppBundle\Entity\Data completed with the data received via POST:

HTTP/1.1 200 OK
Connection: close
Content-type: text/html; charset=UTF-8
Host: 127.0.0.1:8000
X-Powered-By: PHP/5.6.22

AppBundle\Entity\Data Object
(
    [tag] => oi
)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.