input: Write a function to design a web server in Python using the Flask framework that would serve a JSON object containing the current temperature for a city based on the city name given in the request. If the city name is invalid or the request fails, return an error message.
  from flask import Flask, request, jsonify
  import requests
  import json
  
  app = Flask(__name__)
  
  @app.route('/getTemp', methods=['POST'])
  def get_temp():
      try:
          city = request.json['city']
          url = 'http://api.openweathermap.org/data/2.5/weather?q={}&APPID=xxxxxx'
          r = requests.get(url.format(city))
          weather = json.loads(r.text)
          return jsonify({'temperature': weather['main']['temp']})
      except Exception as e:
          return jsonify({'error': str(e)})
  
  if __name__ == '__main__':
      app.run(debug=True)
  
  assert get_temp(request_with_city='New York') == {'temperature': 280.15}
  assert get_temp(request_with_city='InvalidCity') == {'error': 'city not found'}
  assert get_temp(request_with_city='') == {'error': "'city' is a required field"}
output: 
  def get_temp():
      try:
          city = request.json['city']
          url = 'http://api.openweathermap.org/data/2.5/weather?q={}&APPID=xxxxxx'
          r = requests.get(url.format(city))
          weather = json.loads(r.text)
          return jsonify({'temperature': weather['main']['temp']})
      except KeyError:
          return jsonify({'error': "'city' is a required field"})
      except requests.exceptions.RequestException:
          return jsonify({'error': 'city not found'})
      except Exception as e:
          return jsonify({'error': str(e)})
  