{NSErrorFailingURLKey=http://127.0.0.1:8000/Json/default/ricevo, NSLocalizedDescription=Expected status code in (200-299), got 500
def ricevo(): import gluon.contrib.simplejson as sj data=sj.loads(request.body.read()) return()
def ricevo(): from json import loads, dumps data = loads(request.vars.myvar) return dumps(data)If you're usingAFHTTPClient, set theparameterEncodingproperty toAFJSONParameterEncoding. Any method on that HTTP client with aparametersargument will now encode the passed object into a JSON string and set the HTTP body andContent-Typeheader appropriately.
try to print out request.vars and request.vars.body.read() and see their contents.
POST /Json/default/ricevo HTTP/1.1 |
|
| Host | 127.0.0.1:8000 |
| Accept | */* |
| Accept-Encoding | gzip |
| Content-Length | 36 |
| Accept-Language | en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8 |
| Content-Type | application/json; charset=utf-8 |
| Connection | keep-alive |
| User-Agent | mirko.json/1.0 (unknown, iPhone OS 6.0, iPhone Simulator, Scale/1.000000) |
<type 'exceptions.TypeError'>(expected string or buffer)
inspect attributes
Function argument list
(self=<json.decoder.JSONDecoder object>, s=<Storage {}>, _w=<built-in method match of _sre.SRE_Pattern object>)
Code listing
Variables
Context
locals
request
session
response
locals
s
:
<Storage {}>
self
:
<json.decoder.JSONDecoder object>
request
ajax
:
False
application
:
Json
args
:
body
:
<open file '<fdopen>', mode 'w+b' at 0x11c143c00>
client
:
127.0.0.1
controller
:
default
cookies
:
env
:
app_folders
:
set(['/Users/marcoMchurch/Sites/web2py/applications/Json/', '/Users/marcoMchurch/Sites/web2py/applications/welcome/', '/Users/marcoMchurch/Sites/web2py/applications/admin/'])
applications_parent
:
/.…../web2py
cmd_args
:
cmd_options
:
<Values at 0x1104a53f8: {'verbose': False, 'ip': '127.0.0.1', 'shutdown_timeout': 5, 'taskbar': False, 'nocron': False, 'pid_filename': 'httpserver.pid', 'maxthreads': None, 'softcron': False, 'server_name': 'marcos-', 'bpython': False, 'nogui': False, 'port': 8000, 'extcron': False, 'debuglevel': 30, 'test': None, 'folder': '/Users/marcoMchurch/Sites/web2py', 'config': '', 'import_models': False, 'winservice': '', 'shell': None, 'run': '', 'log_filename': 'httpserver.log', 'args': [''], 'socket_timeout': 1, 'ssl_ca_certificate': None, 'scheduler': None, 'profiler_filename': None, 'ssl_private_key': '', 'password': '<ask>', 'request_queue_size': 5, 'ssl_certificate': '', 'cronjob': False, 'numthreads': None, 'quiet': False, 'interfaces': None, 'minthreads': None, 'timeout': 10, 'plain': False, 'nobanner': False}>
content_length
:
36
content_type
:
application/json; charset=utf-8
db_sessions
:
set([])
debugging
:
False
gluon_parent
:
….web2py
http_accept
:
*/*
http_accept_encoding
:
gzip
http_accept_language
:
en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8
http_connection
:
keep-alive
http_content_length
:
36
http_content_type
:
application/json; charset=utf-8
http_host
:
http_user_agent
:
mirko.json/1.0 (unknown, iPhone OS 6.0, iPhone Simulator, Scale/1.000000)
is_jython
:
False
is_pypy
:
False
path_info
:
/Json/default/ricevo
query_string
:
remote_addr
:
127.0.0.1
remote_port
:
60150
request_method
:
POST
script_name
:
server_name
:
marcos-……
server_port
:
8000
server_protocol
:
HTTP/1.1
server_software
:
Rocket 1.2.4
web2py_crontype
:
hard
web2py_path
:
/Users/marcoMchurch/Sites/web2py
web2py_version
:
1
99
4
datetime.datetime(2011, 12, 14, 14, 46, 14)
stable
wsgi_errors
:
<open file '<stderr>', mode 'w' at 0x10f7b2270>
wsgi_file_wrapper
:
<class wsgiref.util.FileWrapper at 0x1103c1328>
wsgi_input
:
<socket._fileobject object at 0x117971950>
wsgi_multiprocess
:
False
wsgi_multithread
:
True
wsgi_run_once
:
False
wsgi_url_scheme
:
http
wsgi_version
:
1
0
extension
:
html
folder
:
/Users/marcoMchurch/Sites/web2py/applications/Json/
function
:
ricevo
get_vars
:
global_settings
:
app_folders
:
set(['/Users/marcoMchurch/Sites/web2py/applications/Json/', '/Users/marcoMchurch/Sites/web2py/applications/welcome/', '/Users/marcoMchurch/Sites/web2py/applications/admin/'])
applications_parent
:
/Users/marcoMchurch/Sites/web2py
cmd_args
:
cmd_options
:
<Values at 0x1104a53f8: {'verbose': False, 'ip': '127.0.0.1', 'shutdown_timeout': 5, 'taskbar': False, 'nocron': False, 'pid_filename': 'httpserver.pid', 'maxthreads': None, 'softcron': False, 'server_name': 'marcos-imac.homenet.telecomitalia.it', 'bpython': False, 'nogui': False, 'port': 8000, 'extcron': False, 'debuglevel': 30, 'test': None, 'folder': '/Users/marcoMchurch/Sites/web2py', 'config': '', 'import_models': False, 'winservice': '', 'shell': None, 'run': '', 'log_filename': 'httpserver.log', 'args': [''], 'socket_timeout': 1, 'ssl_ca_certificate': None, 'scheduler': None, 'profiler_filename': None, 'ssl_private_key': '', 'password': '<ask>', 'request_queue_size': 5, 'ssl_certificate': '', 'cronjob': False, 'numthreads': None, 'quiet': False, 'interfaces': None, 'minthreads': None, 'timeout': 10, 'plain': False, 'nobanner': False}>
db_sessions
:
set([])
debugging
:
False
gluon_parent
:
/Users/marcoMchurch/Sites/web2py
is_jython
:
False
is_pypy
:
False
web2py_crontype
:
hard
web2py_version
:
1
99
4
datetime.datetime(2011, 12, 14, 14, 46, 14)
stable
is_https
:
False
is_local
:
True
is_restful
:
False
now
:
datetime.datetime(2012, 10, 23, 23, 37, 12, 447022)
post_vars
:
url
:
/Json/default/ricevo
utcnow
:
datetime.datetime(2012, 10, 23, 21, 37, 12, 447035)
uuid
:
Json/127.0.0.1.2012-10-23.23-37-12.35f491ef-469f-421f-93e9-0cebff13da2f
vars
:
wsgi
:
environ
:
CONTENT_LENGTH
:
36
CONTENT_TYPE
:
application/json; charset=utf-8
HTTP_ACCEPT
:
*/*
HTTP_ACCEPT_ENCODING
:
gzip
HTTP_ACCEPT_LANGUAGE
:
en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8
HTTP_CONNECTION
:
keep-alive
HTTP_CONTENT_LENGTH
:
36
HTTP_CONTENT_TYPE
:
application/json; charset=utf-8
HTTP_HOST
:
HTTP_USER_AGENT
:
mirko.json/1.0 (unknown, iPhone OS 6.0, iPhone Simulator, Scale/1.000000)
PATH_INFO
:
/Json/default/ricevo
QUERY_STRING
:
REMOTE_ADDR
:
127.0.0.1
REMOTE_PORT
:
60150
REQUEST_METHOD
:
POST
SCRIPT_NAME
:
SERVER_NAME
:
marcos-……
SERVER_PORT
:
8000
SERVER_PROTOCOL
:
HTTP/1.1
SERVER_SOFTWARE
:
Rocket 1.2.4
wsgi.errors
:
<open file '<stderr>', mode 'w' at 0x10f7b2270>
wsgi.file_wrapper
:
<class wsgiref.util.FileWrapper at 0x1103c1328>
wsgi.input
:
<open file '<fdopen>', mode 'w+b' at 0x11c143c00>
wsgi.multiprocess
:
False
wsgi.multithread
:
True
wsgi.run_once
:
False
wsgi.url_scheme
:
http
wsgi.version
:
1
IN FILE: /USERS/MARCOMchurch/SITES/WEB2PY/APPLICATIONS/JSON/CONTROLLERS/DEFAULT.PY
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
# -*- coding: utf-8 -*-
# this file is released under public domain and you can use without limitations
#########################################################################
## This is a samples controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does streaming)
## - call exposes all registered services (none by default)
#########################################################################
……
def ricevo():
from json import loads, dumps
data = loads(request.vars)
print data
print request.vars.body.read()
print request.vars
return dumps(data)
import gluon.contrib.simplejson data = gluon.contrib.simplejson.loads(request.body.read())
import gluon.contrib.simplejson as sj #just if you don't have simplejson in python path
#in that case "import simplejson as sj" works
data = sj.loads(request.body.read())data = sj.loads(request.body.read())
#and later
return dict(db.someDatabase.insert(data))
#or
return dict(db.someDatabase.validate_and_insert(data))
dict(db.someTable.validate_and_insert(**data))
dict(db.someTable.insert(**data))
ps: recent web2py releases should parse application/json POSTs transparently, no need to request.body.read() anymore.... it goes all in request.vars and request.post_vars as usual.