How to use flask request properly to retrieve data from JSON?

تعرفه تبلیغات در سایت

آخرین مطالب

امکانات وب

Vote count: 0

I looked many questions similar to my title but I have not found any that had same problem as me yet.

I did requests.post to post JSON to API restful server. Below is the snippet

import requests

def upload_data():
    url = "http://127.0.0.1:8088/"
    data = {"value":"abc123"}
    response = requests.post(url, data=data)

    print response.status_code, response.reason, response.text

upload_data()

And for the server side

from flask_restful import Api, Resource
from flask import request

class MyAPI(Resource):
    def get():
        pass

    def post(self):
        value = request.data['value']
        response_object = {
            'value':value
        }

        return response_object, 201

I was hoping to get the POST function to work by showing the result of 201 Created with

{
    'value':'abc123'
}

But whenever I run the script, it gives me error saying that

    value = request.data["value"]
TypeError: string indices must be integers, not str

I am sorry if this is a bad question but if anyone could show me what I have been missing in this script, I really appreciate it. Thank you.

asked 44 secs ago

نویسنده : استخدام کار بازدید : 65 تاريخ : پنجشنبه 28 ارديبهشت 1396 ساعت: 19:17

فهرست وبلاگ