0

I am new to flask or web applications, so I have an API running on this a virtual machine using flask, which takes an image as an input and returns the features in a JSON file. Here is the code:

`

from flask import Flask

from flask import send_file

from flask import Flask, request, send_from_directory, redirect, render_template, url_for, jsonify

import socket

import requests

import json

ip = socket.gethostbyname((socket.getfqdn()))

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'


@app.route('/get_image')
def get_image():
    return send_from_directory("C:/images", "22_1.bmp")


@app.route('/goto_api')
def goto_api():
    return redirect("http://127.0.0.1/features/", code=302 )

@app.route('/post')
def post():

url = 'http://127.0.0.1/features/'

payload = {'image': 'http://127.0.0.10:6485/get_image'}
#payload = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"image\"\r\n\r\nhttp://127.0.0.10:6485/get_image\r\n-----011000010111000001101001--"
headers = {'content-type': "application/json"}
response = requests.post(url, data=payload, headers=headers)

return json.loads(str(response)) #return json.loads(response)


if __name__ == '__main__':
    app.debug = True
    app.run(port=6485, host=ip)

`

when I use return json.loads(response), it returns an error: TypeError: the JSON object must be str, bytes or bytearray, not 'Response'

when I use return json.loads(str(response)) it gives the following error:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I have tried many other things as well. Please help :) Here is how output of that API looks like

    HTTP 201 Created
    Allow: GET, POST, OPTIONS
    Content-Type: application/json
    Vary: Accept

{
    "features": [
        0.0,
        0.0,
    4.321675777435303,
    1.5690128803253174,
    0.0,
    4.284165859222412,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    9.667842864990234,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    3.6376543045043945,
    3.540004253387451,
    0.0,
    0.0,
    0.0,
    0.0,
    .
    .
    .
            0.0,
    0.0,
    2.088599681854248,
    5.591969013214111,
    3.5350821018218994,
    0.0,
    0.0,
    13.648137092590332,
    0.0,
    3.5895862579345703,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.9650025963783264,
    0.0,
    0.9726126194000244,
    0.0,
    2.707155704498291,
    0.40982359647750854,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    0.0,
    5.446600437164307
]

}

  • Well the problem is solved when I passed 'json.dumps(payload)' instead of just 'payload' to the 'requests.post()' function and returned it using 'jsonify' as @jsalonen suggested for the flask view function. – Pratik Borhade Jul 05 '17 at 09:58

1 Answers1

1

The type of the response object is not a string, but requests.Response. If you wish to get the response body as JSON you need to use json() method as described in the documentation. For convenience and correct MIME type, you can combine that with Flask's JSON helper flask.json.jsonify:

from flask import jsonify

...

@app.route('/post')
def post():
    ...
    return jsonify(response.json())
jsalonen
  • 26,663
  • 14
  • 82
  • 104
  • I tried that as well, but in that case it gives: { "detail": "JSON parse error - No JSON object could be decoded" } . When I do the post request using urllib2 without using flask, it works. Is there a different way to do the post request using flask ? – Pratik Borhade Jul 04 '17 at 16:34
  • the problem with the post request was solved when I passed 'json.dumps(payload)' to the 'requests.post()' function instead of just 'payload'. Thanks. – Pratik Borhade Jul 05 '17 at 10:01