I'm mocking an Post api (written in C#) which return a bool value true or false when called. The content-type is application/json for the request
true
I am now trying to mock that endpoint in Python using Flask and I'm struggling to make it pass a boolean value.
I tried
return make_response(True,200)
or simply
return True
and in both cases the api fails sending the desired response and throws errors.
In a desperate attempt I tried returning "True" as a string
return make_response("True", 200)
That seemed to work at the mock level, but the consuming code (c#) fails as it tries to convert that return value into bool by
result = response.Content.ReadAsAsync<bool>().Result
Any ideas on how to make the mock api return a bool value ???
You should consider sending json data.
return json.dumps(True)
You're not returning valid JSON. In JSON the boolean is lowercase, "true". You can use json.dumps to generate the correct JSON serialization of a value. You should also set the content type of the response to application/json. Use app.response_class to build a response.
from flask import json
return app.response_class(json.dumps(True), content_type='application/json')
Typically, you would send more than a single value as the response. Flask provides jsonify as a shortcut to return a JSON object with the keys and values you pass it. (It's been improved in Flask dev version to handle other data besides objects.)
from flask import jsonify
return jsonify(result=True, id=id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With