1

I am implementing GET,POST,PUT methods using django 's generic views (django.views.generic.base).

GET and POST methods are working fine. However, the PUT method is not returning any HttpResponse object, although I am getting a code 200 OK from server but no data.

I think this may be the issue related to Cross Origin Resource Sharing (CORS).

Can anyone point me to the right direction to solve this issue ?

I am using nginx + uwsgi for deplyoment.


Edit: I have enabled the nginx server to handle PUT and OPTIONS http verbs.


Here is my PUT code :

def put(self, request, *args, **kwargs):
    """
    Update from the controller with info that all the commands has
    been successfully executed on that controller
    :param mac:
    :param request:
    :param args:
    :param kwargs:
    """
self.true_response["mac"] = None
self.false_response["mac"] = None
if request.method == 'PUT': 
        if "mac" in kwargs:
                mac = kwargs["mac"]
        self.true_response["mac"] = mac
        self.false_response["mac"] = mac
        query = "SELECT COUNT(1) FROM controller WHERE \
            `controller_mac` = '%s'" % mac
            cursor = connections['cnms'].cursor()
            cursor.execute(query)
            result = cursor.fetchall()
            if not result[0][0]:
            return HttpResponse(json.dumps(self.false_response))
        try:
            query = """ UPDATE command SET command_status = 2 WHERE \
                command_mac '%s'""" % mac
            cursor = connections['cnms'].cursor()
            cursor.execute(query)
            return HttpResponse(json.dumps(self.true_response))
        except Exception as error:
            return HttpResponse(json.dumps(self.false_response))
        else:
                return HttpResponse(json.dumps({"status" : "false"}))
else:
    return HttpResponse("Method is Not Supported")
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
dotslash
  • 2,041
  • 2
  • 16
  • 15

1 Answers1

0

So, firstly, your DB query is not secure, depending on what "mac" is supposed to contain. Check out How to use variables in SQL statement in Python? for more details on how to address this.

Secondly do you know where your code branches ?

Does the "mac" kwarg get passed properly to the method?

As far as I am concerned, I also use to specify explicitly the mimetype in json responses, e.g.:

HttpResponse(json_dictionary, mimetype="application/json; charset=utf-8")

Perhaps that may help?

Community
  • 1
  • 1
lai
  • 1,163
  • 10
  • 15