How to start Kafka with API -Part 2

Rui
2 min readApr 18, 2024

--

Follow up with my last post: How to start Kafka with API -Part 1

Last time we go over the set ups of the Kafka and basic flask so that postman can make successful GET or POST

— —

if route looks like this @app.route(‘/api/message’, methods=[‘GET’])

Test GET: go to browser and enter the http://127.0.0.1:5000/api/message

OR use Postman

— —

if route looks like this @app.route(‘/api/message’, methods=[‘POST’])

CONVENTIONAL: no verb is included in the ‘/xxx/xxx’

Test POST: using Postman

OR use CURL in the terminal (I am not a fan of this, cuz I can not see the result anymore after a while)

Detail of the API setting: how to set up more endpoint require parameters

Parameters:

source = request.args.get('source', 'default_source')  
#'source' parameter from URL,
#if not default to 'default_source' if not specified

https://127.0.0.1/api/message?source=xxxx

Now the code looks like this:


@app.route('/api/message', methods=['POST'])
def send_message():
try:
# Get request body
message_value = request.data
source = request.args.get('source', 'default_source')

print(f"Received message from {source}: {message_value}")
# Return success message
return jsonify({"message": "Message sent successfully"}), 200

if __name__ == '__main__':
# Run the Flask app
app.run(debug=True)

This is the terminal output

when not typing any sources then it will use ‘default_source’

--

--