0

I'm studing about RESFful API with python. I want to build a my restful api server, but i have a problem, i don't know how my api server returns proper data by reqeust each parameters

(request code sample) the request code wants to get the information about 'item' : 'sword'

import requests
import json
url = "https://theURL"
querystring={"item":"sword"}
response = requests.request("GET", url, params=querystring)
print (response.json())

(API server response code sample, by flask python)

from flask import Flask, url_for
from flask_restful import Resource, Api, abort, reqparse
app = Flask(__name__)
api = Api(app)

TODOS = {

    "version":"2.0",
    "resultCode":"OK",
    "output":{
         {
        "item" :"sword"
        "price": 300,
        "damage": 20,
         },
        {
        "item" :"gun"
        "price": 500,
        "damage": 30,
         },
        },
}

class Todo(Resource):
    def post(self):
        return TODOS

api.add_resource(Todo, '/item.price')


if __name__ == "__main__":
           app.run(debug=True, host ="192.168.0.8", port = 8080)

So i want to know how i use a code in response api server for returning 'item price' data by reqeusted parameters 'item : sword' I just want to get the selected parameter's item price and damage information.

I thought it might be very easy, i'm tried to search the example but i couldn't find proper sample code.

1

1 Answer 1

0

I'm not a Flask-expert, but this helps setting up and running a minimalistic Flask-server. Then, this explains how to return json-formatted data from your server, and finally how to request and interpret the json-response can be found here. Summarized below:

Server returning a json-formatted data:

from flask import Flask
from flask import jsonify
app = Flask(__name__)

@app.route('/')
@app.route('/index')
def hello():
    return "Hello, World!"


@app.route('/request_sword')
def grant_sword():
    return jsonify({"sword": {"name":"Excalibur","price":"stack overflow"}})

Client requesting json-formatted data:

import json
import urllib.request

url = "http://127.0.0.1:5000/request_sword" ## My local host flask server

data  = urllib.request.urlopen(url)

response = json.loads(data.read())

print(response)

That's all really. You may also just enter the url in your browser, which will correctly read the json-data: enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

So, Should I classify all of items by each 'def' and path? (/'item name')
I'm not sure if I understand your question? I only provided a minimal example: you may extend the REST call /request_sword to take arguments, treat them and return a customized, complex json-data structure.. if that's what you want?
@HeartAimDJ : if the above solution helped you solve your problem, please mark the answer as accepted. If not, please add some more details to your question, explaining what is not working as expected.
I want to create a server that provides different data for each request, for example, request sword, request gun. You wrote code about just one of item, sword

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.