[web2py] Re: Rest Json

2017-01-08 Thread PK


On Saturday, January 7, 2017 at 9:45:15 PM UTC, 黄祥 wrote:
>
> pls try :
> ### Change user with email if username is disabled
> ### Check patterns in 
> http://127.0.0.1:8000/application/controller/api/patterns.json
>
> ## curl
> # GET
> curl -X GET --user user:password -i 
> http://127.0.0.1:8000/application/controller/api/books.json
> # POST
> curl -X POST --user user:password -i -d "name=name0=website0" 
> http://127.0.0.1:8000/application/controller/api/books.json
> # PUT
> curl -X PUT --user user:password -i -d "name=name1=website1" 
> http://127.0.0.1:8000/application/controller/api/books/2.json
> # DELETE
> curl -X DELETE --user user:password -i 
> http://127.0.0.1:8000/application/controller/api/books/2.json
>
> ## Python using requests
> import requests
> from requests.auth import HTTPBasicAuth 
> payload_0 = {"name": "name0", "website": "website0"}
> payload_1 = {"name": "name1", "website": "website1"}
> auth = HTTPBasicAuth('user', 'password')
> # GET
> r = requests.get("
> http://127.0.0.1:8000/application/controller/api/bank.json;, auth = 
> auth).json()
> print r
> # POST
> r = requests.post("
> http://127.0.0.1:8000/application/controller/api/bank.json;, data = 
> payload_0, auth = auth)
> # PUT
> r = requests.put("
> http://127.0.0.1:8000/application/controller/api/bank/2.json;, data = 
> payload_1, auth = auth)
> # DELETE
> r = requests.delete("
> http://127.0.0.1:8000/application/controller/api/bank/2.json;, auth = 
> auth)
>
> best regards,
> stifan
>

Thank.it helps me a lots. i worked Python. it work well.
But My Front End is Android  and i want to know how auth user and password 
login work in restapi .
how should i send data to server .

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Rest Json

2017-01-07 Thread 黄祥
pls try :
### Change user with email if username is disabled
### Check patterns in 
http://127.0.0.1:8000/application/controller/api/patterns.json

## curl
# GET
curl -X GET --user user:password -i 
http://127.0.0.1:8000/application/controller/api/books.json
# POST
curl -X POST --user user:password -i -d "name=name0=website0" 
http://127.0.0.1:8000/application/controller/api/books.json
# PUT
curl -X PUT --user user:password -i -d "name=name1=website1" 
http://127.0.0.1:8000/application/controller/api/books/2.json
# DELETE
curl -X DELETE --user user:password -i 
http://127.0.0.1:8000/application/controller/api/books/2.json

## Python using requests
import requests
from requests.auth import HTTPBasicAuth 
payload_0 = {"name": "name0", "website": "website0"}
payload_1 = {"name": "name1", "website": "website1"}
auth = HTTPBasicAuth('user', 'password')
# GET
r = 
requests.get("http://127.0.0.1:8000/application/controller/api/bank.json;, 
auth = auth).json()
print r
# POST
r = 
requests.post("http://127.0.0.1:8000/application/controller/api/bank.json;, 
data = payload_0, auth = auth)
# PUT
r = 
requests.put("http://127.0.0.1:8000/application/controller/api/bank/2.json;, 
data = payload_1, auth = auth)
# DELETE
r = 
requests.delete("http://127.0.0.1:8000/application/controller/api/bank/2.json;, 
auth = auth)

best regards,
stifan

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Rest Json

2017-01-07 Thread PK


On Saturday, January 7, 2017 at 2:42:31 PM UTC, PK wrote:
>
>
>
> On Thursday, January 5, 2017 at 3:08:46 PM UTC, Leonel Câmara wrote:
>>
>> PK you need to tell the server you're posting JSON in content-type for 
>> instance using $.ajax
>>
>> var somedict = {a: 1, b: 2};
>>
>>
>> $.ajax({
>>
>>  type: "POST",
>>  url: "{{=URL()}}",
>>  dataType: "json", /* if the server is also replying with json*/
>>  contentType: "application/json; charset=UTF-8",
>>  data: JSON.stringify(somedict),
>>  success: function (data) {
>> 
>>  }
>> });
>>
>
> Doing this you will have somedict in request.vars, for instance 
> request.vars.a will be 1
>


thz it helps me alots i have been done.
now i wanna Login to restapi and how should i parse json data 
when get data from server;
eg  curl --user name:password http://../api/books.json
how should i post json data?? 
My Front End is Android Java

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Rest Json

2017-01-07 Thread PK


On Thursday, January 5, 2017 at 3:08:46 PM UTC, Leonel Câmara wrote:
>
> PK you need to tell the server you're posting JSON in content-type for 
> instance using $.ajax
>
> var somedict = {a: 1, b: 2};
>
>
> $.ajax({
>
>  type: "POST",
>  url: "{{=URL()}}",
>  dataType: "json", /* if the server is also replying with json*/
>  contentType: "application/json; charset=UTF-8",
>  data: JSON.stringify(somedict),
>  success: function (data) {
> 
>  }
> });
>


thz it helps me alots i have been done.
now i wanna Login to restapi and how should i parse json data 
when get data from server;
eg  curl --user name:password http://../api/books.json
how should i get json data??

>
>
> Doing this you will have somedict in request.vars, for instance 
> request.vars.a will be 1
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Rest Json

2017-01-05 Thread Leonel Câmara
PK you need to tell the server you're posting JSON in content-type for 
instance using $.ajax

var somedict = {a: 1, b: 2};


$.ajax({

 type: "POST",
 url: "{{=URL()}}",
 dataType: "json", /* if the server is also replying with json*/
 contentType: "application/json; charset=UTF-8",
 data: JSON.stringify(somedict),
 success: function (data) {

 }
});


Doing this you will have somedict in request.vars, for instance 
request.vars.a will be 1

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Rest Json

2017-01-04 Thread PK


On Wednesday, January 4, 2017 at 10:04:31 PM UTC, Anthony wrote:
>
> On Wednesday, January 4, 2017 at 4:42:28 PM UTC-5, 黄祥 wrote:
>>
>> with
>> return response.json(list_of_records)
>>
>> it return a json format even we request it with *.xml
>> curl http://127.0.0.1:8000/a/default/api/test.xml
>> is there a way to have it both json or xml without content variable?
>>
>
> You need to add logic to handle different cases. You could do it via views 
> with different extensions, but then you need a custom JSON view as already 
> noted.
>
> Anthony
>


When I Post json data something  like this 
{[id:1,name:"William",age:20 ]} it uploaded data to server but 
Error shows "Expected Object But not String" its said
how server accept json data format in web2py?

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Rest Json

2017-01-04 Thread Anthony
On Wednesday, January 4, 2017 at 4:42:28 PM UTC-5, 黄祥 wrote:
>
> with
> return response.json(list_of_records)
>
> it return a json format even we request it with *.xml
> curl http://127.0.0.1:8000/a/default/api/test.xml
> is there a way to have it both json or xml without content variable?
>

You need to add logic to handle different cases. You could do it via views 
with different extensions, but then you need a custom JSON view as already 
noted.

Anthony

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Rest Json

2017-01-04 Thread 黄祥
with
return response.json(list_of_records)

it return a json format even we request it with *.xml
curl http://127.0.0.1:8000/a/default/api/test.xml
is there a way to have it both json or xml without content variable?

best regards,
stifan

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Rest Json

2017-01-04 Thread PK


On Wednesday, January 4, 2017 at 4:21:35 PM UTC, Anthony wrote:
>
> Presumably you are relying on the generic.json view. Note, the only way 
> for a view to be triggered is for the controller to return a dictionary, 
> but of course that means the generic.json view will be converting a 
> dictionary to JSON -- there is no way to have it receive and convert a list 
> to JSON. So, you must instead create your own custom JSON view, or more 
> simply, just return JSON directly from the controller. So, instead of:
>
> return dict(content=list_of_records)
>
> do:
>
> return response.json(list_of_records)
>
> Anthony
>
> On Wednesday, January 4, 2017 at 8:21:52 AM UTC-5, PK wrote:
>>
>>
>> 
>> Can i remove "content" from restapi return json ?
>> it gives a little trouble when i m trying to get or post json object.
>>
> Thank it help me A Lot 

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Rest Json

2017-01-04 Thread Anthony
Presumably you are relying on the generic.json view. Note, the only way for 
a view to be triggered is for the controller to return a dictionary, but of 
course that means the generic.json view will be converting a dictionary to 
JSON -- there is no way to have it receive and convert a list to JSON. So, 
you must instead create your own custom JSON view, or more simply, just 
return JSON directly from the controller. So, instead of:

return dict(content=list_of_records)

do:

return response.json(list_of_records)

Anthony

On Wednesday, January 4, 2017 at 8:21:52 AM UTC-5, PK wrote:
>
>
> 
> Can i remove "content" from restapi return json ?
> it gives a little trouble when i m trying to get or post json object.
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Rest Json

2017-01-04 Thread 黄祥
tryin to help but no clue, hehe
*python console work as expected*
>>> d = {"content": [{"a": "1", "b": "2", "c": "3"}]}
>>> l = d.values()
>>> l[0]
[{'a': '1', 'c': '3', 'b': '2'}]

*controllers/default.py*
@request.restful()
def api():
response.view = 'generic.' + request.extension

def GET(*args, **vars):
patterns = 'auto'
parser = db.parse_as_rest(patterns, args, vars)
if parser.status == 200:
d = dict(content = parser.response)
l = d.values()
return l[0]
else:
raise HTTP(parser.status, parser.error)

*produce*
test.idtest.atest.b1ab

not sure what's going on in the background with parse_as_rest

best regards,
stifan

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.