title: How to fix <Response 500> error in python requests?
body: <p>I am using an API, which receives a pdf file and does some analysis, but I am receiving Response 500 always</p>
  
  <p>Have initially tested using Postman and the request goes through, receiving response 200 with the corresponding JSON information. The SSL security should be turned off.</p>
  
  <p>However, when I try to do request via Python, I always get Response 500</p>
  
  <p>Python code written by me:</p>
  
  <pre class="lang-py prettyprint-override"><code>import requests
  
  url = "https://{{BASE_URL}}/api/v1/documents"
  fin = open('/home/train/aab2wieuqcnvn3g6syadumik4bsg5.0062.pdf', 'rb')
  files = {'file': fin}
  r = requests.post(url, files=files, verify=False)
  print (r)
  #r.text is empty
  </code></pre>
  
  <p>Python code, produced by the Postman: </p>
  
  <pre class="lang-py prettyprint-override"><code>import requests
  
  url = "https://{{BASE_URL}}/api/v1/documents"
  
  payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"aab2wieuqcnvn3g6syadumik4bsg5.0062.pdf\"\r\nContent-Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
  headers = {
      'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
      'Content-Type': "application/x-www-form-urlencoded",
      'cache-control': "no-cache",
      'Postman-Token': "65f888e2-c1e6-4108-ad76-f698aaf2b542"
      }
  
  response = requests.request("POST", url, data=payload, headers=headers)
  
  print(response.text)
  </code></pre>
  
  <p>Have masked the API link as {{BASE_URL}} due to the confidentiality</p>
  
  <p>Response by Postman: </p>
  
  <pre><code>{
      "id": "5e69058e2690d5b0e519cf4006dfdbfeeb5261b935094a2173b2e79a58e80ab5",
      "name": "aab2wieuqcnvn3g6syadumik4bsg5.0062.pdf",
      "fileIds": {
          "original": "5e69058e2690d5b0e519cf4006dfdbfeeb5261b935094a2173b2e79a58e80ab5.pdf"
      },
      "creationDate": "2019-06-20T09:41:59.5930472+00:00"
  }
  </code></pre>
  
  <p>Response by Python: </p>
  
  <pre><code>Response&lt;500&gt;
  </code></pre>
  
  <p><strong>UPDATE:</strong> </p>
  
  <p>Tried the GET request - works fine, as I receive the JSON response from it. I guess the problem is in posting pdf file. Is there any other options on how to post a file to an API?</p>
  
  <p>Postman Response RAW: </p>
  
  <pre><code>POST /api/v1/documents
  Content-Type: multipart/form-data; boundary=--------------------------375732980407830821611925
  cache-control: no-cache
  Postman-Token: 3e63d5a1-12cf-4f6b-8f16-3d41534549b9
  User-Agent: PostmanRuntime/7.6.0
  Accept: */*
  Host: {{BASE_URL}}
  cookie: c2b8faabe4d7f930c0f28c73aa7cafa9=736a1712f7a3dab03dd48a80403dd4ea
  accept-encoding: gzip, deflate
  content-length: 3123756
  
  file=[object Object]
  
  HTTP/1.1 200
  status: 200
  Date: Thu, 20 Jun 2019 10:59:55 GMT
  Content-Type: application/json; charset=utf-8
  Transfer-Encoding: chunked
  Location: /api/v1/files/95463e88527ecdc94393fde685ab1d05fa0ee0b924942f445b14b75e983c927e
  api-supported-versions: 1.0
  X-Frame-Options: SAMEORIGIN
  X-XSS-Protection: 1; mode=block
  X-Content-Type-Options: nosniff
  Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
  Referrer-Policy: strict-origin
  
  {"id":"95463e88527ecdc94393fde685ab1d05fa0ee0b924942f445b14b75e983c927e","name":"aab2wieuqcnvn3g6syadumik4bsg5.0062.pdf","fileIds":{"original":"95463e88527ecdc94393fde685ab1d05fa0ee0b924942f445b14b75e983c927e.pdf"},"creationDate":"2019-06-20T10:59:55.7038573+00:00"}
  </code></pre>
  
  <p><strong>CORRECT REQUEST</strong></p>
  
  <p>So, eventually - the correct code is the following:</p>
  
  <pre><code>import requests
  
  files = {
      'file': open('/home/train/aab2wieuqcnvn3g6syadumik4bsg5.0062.pdf', 'rb'),
  }
  response = requests.post('{{BASE_URL}}/api/v1/documents', files=files, verify=False)
  print (response.text)
  </code></pre>