Home > Blockchain >  hashlib "TypeError: 'str' object is not callable"
hashlib "TypeError: 'str' object is not callable"

Time:10-21

I'm trying to build an Flask API in python that returns me the text that I sent to the API in a hashed version. But while hashing I get the following error: "TypeError: 'str' object is not callable"

this is my code:

from flask import Flask, request
from flask_restful import Api, Resource
import hashlib

app = Flask(__name__)
api = Api(app)


class hashing(Resource):
  def get(self, text):
    hash = hashlib.sha256(text('utf-8'))
    text_hashed = hash.hexdigest()
    return {"data":text_hashed}

api.add_resource(hashing, "/hash/<text>")

if __name__ == "__main__":
    app.run(debug=True)

CodePudding user response:

text is the actual string that's passed to the hashing method. I'm guessing you meant to call encode on it, not use it as a function:

hash = hashlib.sha256(text.encode('utf-8'))
  • Related