title: Retrieving JSON in Python in response to POST
body: <p>I'm trying to get a JSON from a server to use it in a Python code. For test purposes, I did <code>POST</code> by <code>curl</code>:</p>
  
  <pre><code>$ curl -u trial:trial -H "Content-Type: application/json"
  -X POST -d '{"BP_TSM":"22"}' http://some-host --trace-ascii -
  </code></pre>
  
  <p>My Java code seems to correctly handle creating JSON as a response. Please look at the result of <code>curl</code> command:</p>
  
  <pre><code> == Info: About to connect() to localhost port 8080 (#0)
  == Info:   Trying ::1...
  == Info: Connected to localhost (::1) port 8080 (#0)
  == Info: Server auth using Basic with user 'trial'
  =&gt; Send header, 224 bytes (0xe0)
  0000: POST /get/auth HTT
  0040: P/1.1
  0047: Authorization: Basic dHJpYWw6dHJpYWw=
  006e: User-Agent: curl/7.29.0
  0087: Host: localhost:8080
  009d: Accept: */*
  00aa: Content-Type: application/json
  00ca: Content-Length: 15
  00de: 
  =&gt; Send data, 15 bytes (0xf)
  0000: {"BP_TSM":"22"}
  == Info: upload completely sent off: 15 out of 15 bytes
  &lt;= Recv header, 23 bytes (0x17)
  0000: HTTP/1.1 202 Accepted
  &lt;= Recv header, 34 bytes (0x22)
  0000: Server: Payara Micro #badassfish
  &lt;= Recv header, 32 bytes (0x20)
  0000: Content-Type: application/json
  &lt;= Recv header, 37 bytes (0x25)
  0000: Date: Thu, 22 Mar 2018 14:30:43 GMT
  &lt;= Recv header, 21 bytes (0x15)
  0000: Content-Length: 108
  &lt;= Recv header, 29 bytes (0x1d)
  0000: X-Frame-Options: SAMEORIGIN
  &lt;= Recv header, 2 bytes (0x2)
  0000: 
  &lt;= Recv data, 108 bytes (0x6c)
  0000: {"title":"Free Music Archive - Albums","message":"","total":"112
  0040: 59","total_pages":2252,"page":1,"limit":"5"}
  {"title":"Free Music Archive - Albums","message":"","total":"11259","total_pages
  
  ":2252,"page":1,"limit":"5"}== Info: Connection #0 to host localhost left intact
  </code></pre>
  
  <p>Now I would like Python script be able to receive the same message that <code>curl</code> did. I wrote the following Python code (note I'm not Python developer):</p>
  
  <pre><code>import pickle
  import requests
  import codecs
  import json
  from requests.auth import HTTPBasicAuth
  from random import randint
  req = requests.get('server/get/auth', auth=HTTPBasicAuth('trial', 'trial'))
  return pickle.dumps(req)
  </code></pre>
  
  <p>Unfortunately, I get error message <code>'unicode' object has no attribute 'copy'</code> when <code>return pickle.dumps(req)</code> command is executed. I also tried using <code>return json.dumps(req)</code> but this time I get another error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/tmp/tmp8DfLJ7/usercode.py", line 16, in the_function
      return json.dumps(req)
    File "/usr/lib64/python2.7/json/__init__.py", line 244, in dumps
      return _default_encoder.encode(obj)
    File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
      chunks = self.iterencode(o, _one_shot=True)
    File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
      return _iterencode(o, 0)
    File "/usr/lib64/python2.7/json/encoder.py", line 184, in default
      raise TypeError(repr(o) + " is not JSON serializable")
  TypeError: &lt;Response [405]&gt; is not JSON serializable
  </code></pre>
  
  <p>Do I have some error in Python code or is it fault of my Java server returning incorrect JSON?</p>