简体   繁体   中英

having issues with posting json to the database - 301 moved permanently symfony

I am creating a webservice in symfony and I am trying to make a post call to the database. On making the post using postman I am getting the error 301 moved permanently. this is my code for the controller

/**
 * @Route("/post/influ/")
 */
public function postAction(Request $request)
 {
   $data = new User;
   $firstname = $request->get('firstname');
   $lastname = $request->get('lastname');
   $username = $request->get('username');
   $gender = $request->get('gender');
   $email = $request->get('email');
   $country = $request->get('country');
   $state = $request->get('state');
   $password = $request->get('password');
   $retypePassword = $request->get('retypePassword');
 if(empty($firstname) || empty($lastname))
 {
   return new View("NULL VALUES ARE NOT ALLOWED", Response::HTTP_NOT_ACCEPTABLE); 
 } 
  $data->setFirstname($firstname);
  $data->setLastname($lastname);
  $data->setUsername($username);
  $data->setGender($gender);
  $data->setEmail($email);
  $data->setCountry($country);
  $data->setState($state);
  $data->setPassword($password);
  $data->setRetypePassword($retypePassword);
  $em = $this->getDoctrine()->getManager();
  $em->persist($data);
  $em->flush();
   return (" Added Successfully", Response::HTTP_OK);
 }

this is the json I am posting to the database using postman but returning 301 moved permanently as a response

{
"id": 3,
"firstname": "xyz",
"lastname": "xyz",
"username": "hello",
"gender": "Male",
"email": "hello@yahoo.com",
"country": "MyContry",
"state": "MyState",
"password": "password",
"retype_password": "password"
}

this is the url I am making the post to

http://localhost:88/HelloApi/web/app_dev.php/post/influ

Please what could be wrong. thanks

I don't have the rep to comment, but if this action is somewhere in the file structure that requires login, the 301 could be moving your webservice over to a login page, which of course it doesn't know what to do with. Do you know where you're getting 301 redirected to?

First, you can return a cleaner response :

<?php
return new Response(301);

I think the redirect problem does not come from your controller but more from your configuration of the security.yml file. Can you copy the access control part here please?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM