Discussion:
[bottlepy] Extract data from "request.json"
Gourinda Adil
2018-09-23 23:32:06 UTC
Permalink
Please I need help, how to extract data from "request.json", does the
syntax is:
-request.json["key"]
or
-request.json.get("key") (as a FormsDic)
Please I am very confused and I am very as my first example with bottle
didn't work :-(
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
gNeandr
2018-09-24 07:41:17 UTC
Permalink
       xP.prefs = json.load(data_file)
   xP.prefs['keyname'] = somedata
Please I need help, how to extract data from "request.json", does the
   -request.json["key"]
      or
   -request.json.get("key") (as a FormsDic)
Please I am very confused and I am very as my first example with
bottle didn't work :-(
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.
---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Marcel Hellkamp
2018-09-24 10:12:14 UTC
Permalink
Post by Gourinda Adil
Please I need help, how to extract data from "request.json", does the
   -request.json["key"]
      or
   -request.json.get("key") (as a FormsDic)
Please I am very confused and I am very as my first example with
bottle didn't work :-(
Why not just test it yourself? What did not work? What is the expected
result, and what did you see instead?

request.json returns a dict, but only of the request actually contained
json data and had the correct content-type header set.

http://bottlepy.org/docs/0.12/api.html#bottle.BaseRequest.json
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Gourinda Adil
2018-09-26 15:38:49 UTC
Permalink
I have a little idea and I want to test is with Bottle:
I want to take the text of a <h1> tag, adding some text and putt it in <p>
tag, and I want to do it with XMLHttpRequest() object:

--- template.tpl ---

<!DOCTYPE html>
<html>

<head>
<title>bottle example</title>

<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(//---begin---
function(){//---begin---
$("p").on(//---begin---
"click"
,function(){//---begin---
var my_request
= new XMLHttpRequest();

my_request.open("POST" ,url:"/hello");
var
request_data = {"h1_value":$("h1").text()};

my_request.setRequestHeader("Content-Type" ,"application/json");

my_request.send(request_data);


$('p').text(my_request.responseText);
}//---end---
)//---end---

}//---end---
)//---end---
</script>
</head>

<body>
<h1>Hello World</h1>
<p></p>
</body>

</html>
-------------------------------------

--- Bottle.py ------------------

from bottle import route ,run ,template ,request

#----- URL:/hello -----
#----- Template -----
@route("/hello")
def example():
return template("template.tpl")
#----- POST+GET -----
@route("/hello", method=['POST' ,'GET'])
def ajax():
json_data = request.json["h1_value"] + "and Welcome to Bottle"
return {"returned_json":json_data}

run(host='127.0.0.1', port=8080, debug=True)
----------------------------------------

But I get this error:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/bottle.py", line 862, in _handle
return route.call(**args)
File "/usr/lib/python3/dist-packages/bottle.py", line 1740, in wrapper
rv = callback(*a, **ka)
File "Bottle.py", line 17, in ajax
json_data = request.json["h1_value"] + "and Welcome to Bottle"
TypeError: 'NoneType' object is not subscriptable

Please if you can help me Because I wasn't able to resolve this problem since 1 week and it begin to make me crasy.

Thanks for your help
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...