dannyxn
dannyxn

Reputation: 422

Flask REST API fake POST request

During unit tests of REST Flask server I've encountered following problem: I have a view function that should handle POST request with collection of numbers ( Sample without function body )

@api.route("/last_op/add", methods=["GET", "POST"])
def post_add_last_operation():
    return request.data

For testing purposes I use Flask.test_client() object.

When trying to send POST request in following way:

app = Flask(__name__)
client = app.test_client()
client.post("/last_op/add", data={"collection": "1,2,3,4"})

I am receiving empty return value. What am I missing? Thanks in advance

Upvotes: 0

Views: 287

Answers (1)

Roney Gomes
Roney Gomes

Reputation: 507

Try using request.get_data() instead.

If that still doesn't work, you can try passing your payload as JSON

client.post('/last_op/add', json={'collection': '1,2,3,4'})

then in the handler use

make_response(jsonify(request.get_json()))

to build your response.

Upvotes: 1

Related Questions