title: How can I add an order with POST request in the RESTful Service?
body: <p>I am trying to add an order for customer's panel with JWT authentication.
  I activated authorization,add parameters in the body in Postman and send it but &quot;status&quot;:422,&quot;errors&quot;:&quot;Order no valid&quot;} it gives this message and dev.log file is below.</p>
  <pre><code>[2020-08-24T10:31:01.791369+02:00] request.INFO: Matched route &quot;order_apiorders_add&quot;. {&quot;route&quot;:&quot;order_apiorders_add&quot;,&quot;route_parameters&quot;:{&quot;_route&quot;:&quot;order_apiorders_add&quot;,&quot;_controller&quot;:&quot;App\\Controller\\OrderController::addOrder&quot;},&quot;request_uri&quot;:&quot;http://localhost:8000/api/orders&quot;,&quot;method&quot;:&quot;POST&quot;} []
  [2020-08-24T10:31:01.823528+02:00] security.DEBUG: Checking for guard authentication credentials. {&quot;firewall_key&quot;:&quot;api&quot;,&quot;authenticators&quot;:1} []
  [2020-08-24T10:31:01.823621+02:00] security.DEBUG: Checking support on guard authenticator. {&quot;firewall_key&quot;:&quot;api&quot;,&quot;authenticator&quot;:&quot;Lexik\\Bundle\\JWTAuthenticationBundle\\Security\\Guard\\JWTTokenAuthenticator&quot;} []
  [2020-08-24T10:31:01.823697+02:00] security.DEBUG: Calling getCredentials() on guard authenticator. {&quot;firewall_key&quot;:&quot;api&quot;,&quot;authenticator&quot;:&quot;Lexik\\Bundle\\JWTAuthenticationBundle\\Security\\Guard\\JWTTokenAuthenticator&quot;} []
  [2020-08-24T10:31:01.836138+02:00] security.DEBUG: Passing guard token information to the GuardAuthenticationProvider {&quot;firewall_key&quot;:&quot;api&quot;,&quot;authenticator&quot;:&quot;Lexik\\Bundle\\JWTAuthenticationBundle\\Security\\Guard\\JWTTokenAuthenticator&quot;} []
  [2020-08-24T10:31:01.937527+02:00] doctrine.DEBUG: SELECT t0.id AS id_1, t0.username AS username_2, t0.password AS password_3 FROM user t0 WHERE t0.username = ? LIMIT 1 [&quot;admin&quot;] []
  [2020-08-24T10:31:01.949440+02:00] security.INFO: Guard authentication successful! {&quot;token&quot;:{&quot;Lexik\\Bundle\\JWTAuthenticationBundle\\Security\\Authentication\\Token\\JWTUserToken&quot;:&quot;JWTUserToken(user=\&quot;admin\&quot;, authenticated=true, roles=\&quot;ROLE_USER\&quot;)&quot;},&quot;authenticator&quot;:&quot;Lexik\\Bundle\\JWTAuthenticationBundle\\Security\\Guard\\JWTTokenAuthenticator&quot;} []
  [2020-08-24T10:31:01.950400+02:00] security.DEBUG: Guard authenticator set no success response: request continues. {&quot;authenticator&quot;:&quot;Lexik\\Bundle\\JWTAuthenticationBundle\\Security\\Guard\\JWTTokenAuthenticator&quot;} []
  [2020-08-24T10:31:01.950461+02:00] security.DEBUG: Remember me skipped: it is not configured for the firewall. {&quot;authenticator&quot;:&quot;Lexik\\Bundle\\JWTAuthenticationBundle\\Security\\Guard\\JWTTokenAuthenticator&quot;} []
  [2020-08-24T10:31:01.966109+02:00] doctrine.DEBUG: &quot;START TRANSACTION&quot; [] []
  [2020-08-24T10:31:01.967877+02:00] doctrine.DEBUG: INSERT INTO order (product_id, quantity, address, shipping_date, user_id) VALUES (?, ?, ?, ?, ?) {&quot;1&quot;:1,&quot;2&quot;:1,&quot;3&quot;:&quot;Levent&quot;,&quot;4&quot;:20,&quot;5&quot;:32} []
  [2020-08-24T10:31:01.971845+02:00] doctrine.DEBUG: &quot;ROLLBACK&quot; [] []
  </code></pre>
  <p>I have two entity classes which are User and Order and User's variables are &quot;username,password,orders(orders variable has a OneToMany relationship and mappedBy=&quot;user&quot;). Order's variables are &quot;productId,quantity,address,shippingDate and user(user has a ManyToOne relationship and inversedBy=&quot;orders&quot;)</p>
  <p>My addOrder function is below,</p>
  <pre><code>/**
   * @param Request $request
   * @param TokenStorageInterface $tokenStorage
   * @return JsonResponse
   * @Route(&quot;/orders&quot;, name=&quot;orders_add&quot;, methods={&quot;POST&quot;})
   */
  public function addOrder(Request $request,TokenStorageInterface $tokenStorage)
  {
      $entityManager = $this-&gt;getDoctrine()-&gt;getManager();
  
      try{
          $request = $this-&gt;transformJsonBody($request);
  
          if (!$request || !$request-&gt;request-&gt;get('productId') || !$request-&gt;request-&gt;get('quantity') || !$request-&gt;request-&gt;get('address') || !$request-&gt;request-&gt;get('shippingDate')){
              throw new Exception();
          }
  
          $user = $tokenStorage-&gt;getToken()-&gt;getUser();
          $order = new Order();
  
          $order-&gt;setProductId($request-&gt;get('productId'));
          $order-&gt;setQuantity($request-&gt;get('quantity'));
          $order-&gt;setAddress($request-&gt;get('address'));
          $order-&gt;setShippingDate($request-&gt;get('shippingDate'));
          $order-&gt;setUser($user);
          $entityManager-&gt;persist($order);
          $entityManager-&gt;flush();
  
  
          $data = [
              'status' =&gt; 200,
              'success' =&gt; &quot;Order added successfully&quot;,
          ];
          return $this-&gt;response($data);
  
      }catch (Exception $e){
          $data = [
              'status' =&gt; 422,
              'errors' =&gt; &quot;Order no valid&quot;,
  
          ];
          return $this-&gt;response($data, 422);
      }
  }
  </code></pre>
  <p>In addition,link of github repo is <a href="https://github.com/Berkayermis/RESTfulService" rel="nofollow noreferrer">https://github.com/Berkayermis/RESTfulService</a></p>
  <p>I have read lots of articles and talked many people but I am learning REST APIs since last month and I am very newbie on this topic but I want to learn it.</p>
  <p>Thanks</p>