title: How do I fix a HTTP Error 400: Bad Request in python3?
body: <p>I am trying to access checkpoint firewall by using it's API but for some reason I am getting HTTP Error 400: Bad Request, I have never had this before.
  Any ideas?
  Here is my code:</p>
  <pre><code>import json
  import ssl
  import urllib.request, urllib.parse, urllib.error
  import urllib.request, urllib.error, urllib.parse
  import requests
  from requests.packages.urllib3.exceptions import InsecureRequestWarning
  requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  sslctx = ssl.create_default_context()
  sslctx.check_hostname = False
  sslctx.verify_mode = ssl.CERT_NONE
  
  def checkpoint_api_call(cp_ip, port, command, json_payload, sid):
      apiurl = 'https://' + cp_ip + '/web_api/' + command
      req = urllib.request.Request(apiurl)
      if sid == '':
          req.add_header('Content-Type', 'application/json')
      else:
          req.add_header('Content-Type', 'application/json')
          req.add_header('X-chkp-sid', sid)
      try:
          data1 = urllib.parse.urlencode(json_payload).encode(&quot;utf-8&quot;)
          resp = urllib.request.urlopen(req, data=data1, context=sslctx)
          res_code = resp.getcode()
          return resp_output, res_code
      except urllib.error.HTTPError as e:
          print((str(e)))
  main()
      payload = {'user': admin, 'password': password}
      response, res_code = checkpoint_api_call(x.x.x.x, 443, 'login', payload, '')
  </code></pre>
  <p>basically same thing is working fine with python 2</p>
  <p>this is python2 code</p>
  <pre><code>import ssl
  import urllib
  import urllib2
  import requests
  from requests.packages.urllib3.exceptions import InsecureRequestWarning
  requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  sslctx = ssl.create_default_context()
  sslctx.check_hostname = False
  sslctx.verify_mode = ssl.CERT_NONE
  def checkpoint_api_call(cp_ip, port, command, json_payload, sid):
      apiurl = 'https://' + cp_ip + '/web_api/' + command
      jencode = json.dumps(json_payload)
      resp_output = &quot;&quot;
      req = urllib2.Request(apiurl)
      if sid == '':
          req.add_header('Content-Type', 'application/json')
      else:
          req.add_header('Content-Type', 'application/json')
          req.add_header('X-chkp-sid', sid)
      try:
          resp = urllib2.urlopen(req, data=jencode, context=sslctx)
          res_code = resp.getcode()
          resp_output = json.load(resp)
          return resp_output, res_code
      except urllib2.HTTPError as e:
          res_code = e.getcode()
          print e.getcode()
          return resp_output, res_code
  
  main()
      payload = {'user': user, 'password': password}
      response, res_code = checkpoint_api_call(x.x.x.x, 443, 'login', payload, '') 
  </code></pre>