0

I am trying to retrieve data from url into json format using flask-restplus.

from flask import Flask, render_template
import requests
import json
from flask_restplus import Resource, Api, fields
from flask_sqlalchemy import SQLAlchemy

# config details
COLLECTION = 'indicators'

app = Flask(__name__)
api = Api(
  app,
  title='Akhil Jain',
  description='Developing ' \
  'a Flask-Restplus data service that allows a client to ' \
  'read and store some publicly available economic indicator ' \
  'data for countries around the world, and allow the consumers ' \
  'to access the data through a REST API.'
)

@api.route('/indicators')
def get(self):
        uri = 'http://api.worldbank.org/v2/indicators'
        try:
            res = requests.get(uri)
            print(res)
            return res.json()
        except:
            return False

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

but after trying out the GET, the response i am getting is False instead of the json data. Could anyone tell me how to get the response data so that i can process it to sqllite db.

Thanks

4
  • It seems like api.worldbank.org/v2/indicators response is not json Commented Mar 17, 2019 at 6:13
  • @a.l. yes, i think it is XML Commented Mar 17, 2019 at 6:18
  • thus return res.json() would raise an Exception which would make your app response False Commented Mar 17, 2019 at 6:24
  • did the solution that i provided work ?. Commented Mar 18, 2019 at 7:41

2 Answers 2

1

you have to get the content from response

If you would want to save the xml data then.

try:
            res = requests.get(uri)
            print(res.content)
            return res.content
         except:
            return False

If you want to save it as json, then install module xmltodict.

try:
            res = requests.get(uri)
            jsondata = xmltodict.parse(res.content)
            print(jsondata)
            return jsondata
         except:
            return False
Sign up to request clarification or add additional context in comments.

Comments

0

You're trying to retrieve an XML producing endpoint. - Invoking json will raise an exception since the content retrieved is not json.

The ideal way of doing this is to build a function that parses the data as per your requirements; the example below can be treated as a reference.

import xml.etree.ElementTree as element_tree
xml = element_tree.fromstring(response.content)

for entry in xml.findall(path_here, namespace_here):
    my_value = entry.find(attribute_here, namespace_here).text 

You can opt to use lxml or other tools available. - The above is an example of how you can parse your xml response.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.