Question: How can I response a JSON with render='json',and set the header

379 views
Skip to first unread message

Zane

unread,
Feb 20, 2012, 9:55:26 AM2/20/12
to pylons...@googlegroups.com
Sample:
@view_config(permission = 'view',route_name='addUser', renderer='json', custom_predicates=(allowed_methods('POST'),))
def add(request):
     post_data = request.json_body
     email = post_data['email']
     headers = remember(request, email, max_age='86400')

     /* How to response this header together*/
     return {'succeed':True}

Thanks
Zane

Chris McDonough

unread,
Feb 20, 2012, 10:07:14 AM2/20/12
to pylons...@googlegroups.com

I think this is what you're asking:

request.response.headers['Foo'] = 'bar'

- C


>
> Thanks
> Zane
> --
> You received this message because you are subscribed to the Google
> Groups "pylons-devel" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/pylons-devel/-/D5fWCOMEDOYJ.
> To post to this group, send email to pylons...@googlegroups.com.
> To unsubscribe from this group, send email to pylons-devel
> +unsub...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-devel?hl=en.


Zane

unread,
Feb 20, 2012, 10:34:11 AM2/20/12
to pylons...@googlegroups.com
Thanks @Chris

Yes.This is what I want, but now I receive a string "{'succeed':True}", and before I add header in response is a object  {'succeed':True} ( I using Ajax)
I guess this cause the view_config(renderer='json')

But How can I add header and return a Obj not a string.

Sorry~ I'm a newer.And Thank you.
Zane

Chris McDonough

unread,
Feb 20, 2012, 10:46:35 AM2/20/12
to pylons...@googlegroups.com

I'm afraid I don't understand, sorry.

- C


>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "pylons-devel" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/pylons-devel/-/IxdaCNua2XYJ.

Zane

unread,
Feb 20, 2012, 11:05:03 AM2/20/12
to pylons...@googlegroups.com
Oh~ Excuse my English~

May be I know the reason of my fault,
I am using  pyramid.security

from pyramid.security import authenticated_userid, remember, forget

@view_config(route_name='user', renderer='json')
     ..............
     headers = remember(request, email, max_age='86400')

     request.response.headers = headers
     # It would lost the Content-Type:application/json here
     # so when I add header like this way, it will return a string.
    return {'succeed':True}

And before I replace the header, the render='json' will help me to add the Content-Type.

Thanks
Zane



cheeleong

unread,
Feb 20, 2012, 11:08:58 AM2/20/12
to pylons...@googlegroups.com
Try this

@view_config(route_name='user', renderer='json')
def user(request):
  headers = remember(request, email)
  request._response_headerlist_set(headers)
  return {'foo':'bar'}

Good luck.

Regards,
CL Chow

Using Gmail? Please read this important notice:





Thanks
Zane



--
You received this message because you are subscribed to the Google Groups "pylons-devel" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pylons-devel/-/8pnmgKfyTVoJ.

To post to this group, send email to pylons...@googlegroups.com.
To unsubscribe from this group, send email to pylons-devel...@googlegroups.com.

Zane

unread,
Feb 20, 2012, 11:16:36 AM2/20/12
to pylons...@googlegroups.com
It's work  :)

Thank you @ yasaikun and  Chris McDonough


cheeleong

unread,
Feb 20, 2012, 11:25:36 AM2/20/12
to pylons...@googlegroups.com
No problem :)

Regards,
CL Chow

Using Gmail? Please read this important notice:




On Tue, Feb 21, 2012 at 12:16 AM, Zane <yaoa...@gmail.com> wrote:
It's work  :)

Thank you @ yasaikun and  Chris McDonough

--
You received this message because you are subscribed to the Google Groups "pylons-devel" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pylons-devel/-/_x-s9OJqdhYJ.
Reply all
Reply to author
Forward
0 new messages