Search code examples
python-3.xurllib

how to fix 'This QueryDict instance is immutable' when send http post by urllib.request


I'm writting a sender by urllib.request, but get "This QueryDict instance is immutable" in server.

It's fixed when I add "bundle.request.POST._mutable = True" in server code(django tastypie).

But I want to know how to fix by on sender.

python:v3.6 sender: urllib.request server: django tastypie

sender:

   cookie = http.cookiejar.MozillaCookieJar()
   handler = urllib.request.HTTPCookieProcessor(cookie)
   opener = urllib.request.build_opener(handler)
   request = urllib.request.Request(url, 
                  data=data,headers=headers,method=method)
   rep = opener.open(request)

erros in server:

File "/python3.6/site-packages/tastypie/resources.py", line 1408, in post_list
    updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))
  File "/python3.6/site-packages/tastypie/resources.py", line 2244, in obj_create
    bundle = self.full_hydrate(bundle)
  File "/python3.6/site-packages/tastypie/resources.py", line 943, in full_hydrate
    bundle = self.hydrate(bundle)
  File "/home/ma/*/api.py", line 115, in hydrate
    bundle.data["create_by"] = user
  File "/python3.6/site-packages/django/http/request.py", line 421, in __setitem__
    self._assert_mutable()
  File "/python3.6/site-packages/django/http/request.py", line 418, in _assert_mutable
    raise AttributeError("This QueryDict instance is immutable")

Solution

  • i find a reason, application/x-www-form-urlencoded -> application/json.