Search code examples
pythonweb-servicescachingflaskflask-restful

python - how to disable cache in Flask? Getting 500 server error during second POST request sent to server while first request was 200 OK


I 'think' I have some caching issue. my webservice is receiving data through POST request and saving it to disk.

Following is my code:

@app.route('/ws_part' , methods=['POST'])
def ws_part():
    request_data = request.get_json()

    #placeholder for workstation number and part number 
    received_data ={'ws_no':request_data['workstation'],
                'part_no':request_data['part']}

    #Checking if workstation number is already available
    global updated
    updated = 'no'
    for i in repository:
        if i['ws'] == received_data['ws']:
            i['part'] = received_data['part']
            updated = 'yes'

    if(updated!='yes'):
        new_input = received_data
        repository.append(new_input)

    return jsonify({'repository': repository})

Issue I am facing: 1.The very first request goes through successfully and gives 200 OK response and workstation number and part coming with the request get saved in 'repository' placeholder. 2.But the very next request throws 500 server error

Error: File "API_ws_par.py", line 23, in ws_part if i['ws'] == received_data['ws']: KeyError: 'ws'

3.Funny thing happening is: If at this point I restart my web service and trigger the POST request again, the data coming with this new request is getting overwritten and I am losing my previously saved data. 4.Also, the consequent second POST request throws the same 500 server error

Please, guide if this is something to do with cache? If yes, then please let me know how to do it?

Thanks in advance.


Solution

  • this is not a caching problem, it's a problem in the logic of the function, on the first call, you probably don't get to the line of code generating the exception, The problem is on the second call, the key name is not 'ws' it's 'ws_no', if you change these lines:

      for i in repository:
        if i['ws'] == received_data['ws_no']:
            i['part'] = received_data['part_no']
            updated = 'yes'
    

    to use 'ws_no', and 'part_no' you won't have the key error.