Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getting 'The HTTP status code "0" is not valid' exception #418

Closed
arashdalir opened this issue Apr 3, 2013 · 3 comments
Closed

getting 'The HTTP status code "0" is not valid' exception #418

arashdalir opened this issue Apr 3, 2013 · 3 comments

Comments

@arashdalir
Copy link

Hi everyone!
I'm using FOSRestBundle to manage a profile edit form and am getting

The HTTP status code "0" is not valid

when trying to send redirection response to the client.

I've configured fos_rest as followed:

fos_rest:
    format_listener:
        default_priorities: ['json', "xml", html, '*/*']
    view:
        formats:
            xml: true
            json: true
        templating_formats:
            html: true
        force_redirects:
            html: false
        failed_validation: HTTP_BAD_REQUEST
        default_engine: twig
        view_response_listener: 'force'
    service:
        serializer: jms_serializer.serializer
    serializer:
        version: ~
        groups: []

my respective action function is as followed:

public function partialEditAction(Request $request)
    {
        $user = $this->authenticate();
        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
        $dispatcher = $this->container->get('event_dispatcher');

        $event = new GetResponseUserEvent($user, $request);
        $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);

        if (null !== $event->getResponse()) {
            return $event->getResponse();
        }

        $action = $request->get('part');

        $type = null;
        switch ($action){
        case 'personalInfo':
            $type = new PersonalInfoFormType();
            break;

        case 'email':
            $type = new EmailFormType();
            break;

        case 'password':
            $type = new PasswordFormType();
            break;
        }

        $form = $this->createForm($type, $user);
        //$form->set
        //$form->setData($user);

        if ('POST' === $request->getMethod()) {
            $form->bind($request);

            if ($form->isValid()) {
                /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
                $userManager = $this->container->get('fos_user.user_manager');

                //$event = new FormEvent($form, $request);
                //$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);

                $userManager->updateUser($user);

                //if (null === $response = $event->getResponse())
                {
                    $view = $this->routeRedirectView('fos_user_profile_show', array(), Codes::HTTP_SEE_OTHER);
                    //$view->setStatusCode(Codes::HTTP_NO_CONTENT);
                }

                //$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

                return $this->handleView($view);
            }
        }

        $view = $this->view();
        $view->setData( array('form' => $form) );
        $view->setTemplate('TaraUserBundle:Profile:form.html.twig');

        return $this->handleView($view);
    }

as you can see, I'm trying to create a routeRedirectResponse with Codes::HTTP_SEE_OTHER as the status code and fos_user_profile_show as the route name. I'm trying to read the response later on the client side and according to the status code (204, 200, etc.) do an action. if the response is HTTP_SEE_OTHER, I'd be reading the location header value to find the redirection location and handle the request manually...
but FOSRestBundle keeps throwing the "InvalidArgumentException":

The HTTP status code "0" is not valid.

500 Internal Server Error - InvalidArgumentException

I traced the error to FOS\RestBundle\View\ViewHandler.php line: 274 :

        $code = isset($this->forceRedirects[$format])
            ? $this->forceRedirects[$format] : $this->getStatusCode($view, $content);

        $response->setStatusCode($code);

this line checks the value of my fos_rest config, find the force_redirects value for html format to be "false" and simply replaces my given status_code with a "false" value; which later on results in the http status code to be "0".

I'm not sure if this means that I have misconfigured the bundle or this could actually be a bug in the system. Could anyone please advise me on this matter?

@lsmith77
Copy link
Member

lsmith77 commented Apr 4, 2013

maybe there is a bug in your routeRedirectView method?

@lsmith77
Copy link
Member

ping

@mirihen
Copy link

mirihen commented Jan 14, 2020

The HTTP status code "0" is not valid
mon code
if ($this->request->get('type')) {
$this->ticket->setType($this->request->get('type'));
}

    if ($this->request->get('valeur')) {
        $this->ticket->setValeur($this->request->get('valeur'));
    }
    

    $this->apiEntityManager->persist($this->ticket);
    $this->apiEntityManager->flush();

    return ['data' => [
            'messages' => 'update_success ',
            'code' => $this->ticket->getCode(),
    ]];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants