简体   繁体   中英

How to get json data from a url using flask in python

I am developing an openerp module. Someone makes a POST to a url.the POST contains json data:

{
"phone":"987654321",
"message":"this is a test",
"date": "2015/10/09 12:00:00",
}

I want to get the data from the URL,and handle the parameters inside the openerp. This is my code so far :

from flask import Flask, request
app = Flask(__name__)
    class myClass():
        def __init__(self):
            self.HOST = "http://0.0.0.0:8069"
            self.DB = 'database'
            self.LOGIN = 'admin'
            self.PASSWORD = 'admin'

            self.phone = None
            self.message = None
            self.date = None

    def authenticate(self):
            p = {'jsonrpc': "2.0",
                 'method': "call",
                 'params': {
                     'db': self.DB,
                     'login': self.LOGIN,
                     'password': self.PASSWORD}
                 }

@app.route("/post", methods=['GET', 'POST'])
def post():

    sent_obj = myClass()

    sent_obj.phone = request.args.get('phone')    
    sent_obj.message = request.args.get('message')
    sent_obj.date = request.args.get('date')

if __name__ == "__main__":
    app.debug = True
    app.run()

How can i use flask, so i can get the data from the url, authenticate openerp, and handle the data inside the openerp?What is the procedure i have to follow?I am really confused...

Use json.loads() to read and decode the data.

from flask import Flask,jsonify
import requests
import simplejson 
import json

app = Flask(__name__)

@app.route("/")
def home():
    uri = "https://api.stackexchange.com/2.0/users?   order=desc&sort=reputation&inname=fuchida&site=stackoverflow"
    try:
        uResponse = requests.get(uri)
    except requests.ConnectionError:
       return "Connection Error"  
    Jresponse = uResponse.text
    data = json.loads(Jresponse)

    displayName = data['items'][0]['display_name']# <-- The display name
    reputation = data['items'][0]['reputation']# <-- The reputation

    return Jresponse

if __name__ == "__main__":
    app.run(debug = True)

使用以下代码:

json.loads(request.data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM