How to use REST to validate the receipt of XML via POST

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. Listening to say it would be via Rest but I've never used Rest.

I would like guidance or guidance for Symfony 3 and preferably in Portuguese.

    
asked by anonymous 11.07.2016 / 14:09

2 answers

2

Your question is pretty broad, so I'll explain "over the top" what you can do if the idea is to really use Symfony 3:

  • 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);
  • create a class AppBundle\Entity\Data with the data you will receive (I put an example below);
  • modify the single route to receive the request data and map it to the class created above.
  • Enable the serializer service in its config.yml (example below);
  • run your project with the command bin/console server:start
  • Test the route with a POST request through HTTPie : echo '<data><tag>oi</tag></data>' | http 127.0.0.1:8000/
  • File config.yml :

    framework:
        serializer:
            enabled: true
    

    (ps: You do not need to delete all settings from the framework key, just add the serializer setting).

    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 class AppBundle\Entity\Data populated with 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
    )
    
        
    12.07.2016 / 11:26
    3

    My dear, in this case you do not have a cake recipe, ideally you should go to the Symfony documentation ( link ) and take a look at what the framework makes available to you.

    As in your case it is a REST API, I strongly recommend using the symfony-based micro framework called Silex ( link ). Because it is a micro-framework, it is much leaner, bringing the essentials to build an API Rest with a powerful stack.

    Note: The links are in English, but you can not get away from it. Good luck!

        
    11.07.2016 / 15:07