1

I wrote a POST endpoint in Python Flask to get the response OK as a string value (without quotes). However, when the API is tested the API responds with "OK" (with quotes). The results in Ipkiss Tester is the following:

*❌ Reset state before starting tests
POST /reset
Expected: 200 OK
Got:      200 "OK"

Any suggestions to fix this issue?.

Thanks.

Below the code:

from flask import Flask, request, jsonify
from flask_restful import abort, Api, reqparse, Resource
from flask_api import status

class AccountModel:
    def __init__(self, accnum):
        # We will automatically generate the new id
        self.id = accnum
        self.balance = 0

class AccountManager():
    def __init__(self):
        self.accounts = {}

    def delete_accounts(self):
        if not bool(self.accounts):
            del self.accounts
        self.accounts = {}


account_manager = AccountManager()

class ResetAccount(Resource):
    def post(self):
        if str(request.url_rule) =="/reset":
            account_manager.delete_accounts()
            return   "OK", status.HTTP_200_OK
        else:
            abort(
                status.HTTP_404_NOT_FOUND,
                message=str(status.HTTP_404_NOT_FOUND) + " " + "0".format())


app = Flask(__name__)
service = Api(app)

service.add_resource(ResetAccount, '/reset', endpoint='reset_endpoint')


if __name__ == '__main__':
    app.run(debug=True)
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Your code is not returning `"OK"` with strings. I don't know what Ipkiss Tester is or what it is expecting, but my guess is that it expects a HTTP 200 with no contents (the OK in `200 OK` is probably just the description of code 200). – zvone Oct 13 '20 at 00:02
  • On the other hand, it is strange that a POST would result in 200 with no content. HTTP 204 is used for no content and I would usually return either that or 303. – zvone Oct 13 '20 at 00:05
  • For future reference, there's no need to post another answer which is just saying that a solution described in the linked post worked. A closure as duplicate already implies that. – wim Oct 13 '20 at 05:07

1 Answers1

1

Than you @zvone for your comments.

It was solved based on the solution described in How to return plain text from flask endpoint? Needed by Prometheus.

The solution was to use "flask.make_response" and define "response.mimetype = "text/plain"" as is explained in the reference