Importing httplib Library

Hello, can some one help explain or provide insights on why I receive the following error when I try to import httplib library?

image

This is the actual error. As for why it’s happening, no idea. Some google suggests possibly firewall restrictions, but it’s literally impossible to say without the full details.

Why are you trying to import httplib? I would recommend using system.net.httpClient over any Jython/Python module, for performance and stability (like this) reasons.

That makes sense.

I would prefer to use system.net.httpClient. However, I am having difficulty converting my code using httplib into the appropriate code for system.net.httpClient. I am new to making API calls and this function so I am sure I am missing something obvious between the two…

This is my code using httplib

def getBearerToken():
	conn = httplib.HTTPSConnection("")
	payload = json.dumps({
	  "username": "***",
	  "password": "***",
	  "siteId": "**"
		})
	headers = {
	  'Content-Type': 'application/json',
	  'Accept': 'application/json',
	  'Host': '***.cox2m.com'
		}
	conn.request("POST", "/v1/login", payload, headers)
	res = conn.getresponse()
	data = json.loads(res.read())
	return data["idToken"]

This is my system.net.httpClient code:

url="https://***.cox2m.com/v1/login"
payload = json.dumps({
	  "username": "**",
	  "password": "***",
	  "siteId": "**"
		})
headerVals = {
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Host': '***.cox2m.com'
	}
client = system.net.httpClient()
res = client.get(url, data=payload, headers=headerVals)

httpClient will handle JSON negotiation and encoding for you:

url="https://***.cox2m.com/v1/login"
payload = {
	  "username": "**",
	  "password": "***",
	  "siteId": "**"
		}
headerVals = {
  'Host': '***.cox2m.com'
	}
client = system.net.httpClient()
res = client.get(url, data=payload, headers=headerVals)
data = response.json
return data["idToken"]

Okay, thank you! Is there a different way you have to treat the Host header value since it’s restricted?

Hm. Is it actually necessary? Try running your request without specifying it at all.

If you do need it, there aren’t a lot of options unfortunately…

Hm. I’m assuming so… When I ran it without specifying I got the following result:

>>> 
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Track & Trace</title>
  <link href="" rel="shortcut icon" type="image/x-icon">
  <meta name="robots" content="noindex" />
  <meta content="Table Map Template" property="og:title">
  <meta content="width=device-width, initial-scale=1" name="viewport">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
  <!-- Pendo snippet -->
  <script type="text/javascript" src="https://cdn.pendo.io/agent/static/0038a5f4-ba14-4e16-4a7d-0adb1df0c3da/pendo.js"></script>
</head>

<body>
  <div id="root"></div>
  <div id="modal-root"></div>
  
    <script src="/bundle.6317b52c6a157dd346fc.js"></script>
    
      <script>
        Application.start()
      </script>
</body>

</html>

>>>

When I am expecting one similar to this:

"sourceUser": {
        "profile": {
            "id": "2BvvMzlgZGHx5XSjCmcUxv",
            "username": "***",
            "name": "***",
            "email": "******",
            "created": 1653426352141,
            "lastLogin": 1655130403642,
            "lastVerify": 1655130403642,
            "permissions": {
                "role": "user"
            },
            "projectId": "4ab8Dh3jNXiV1QmMf6xffI",
            "metadata": null
        },
        "idToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwcmoiOiI0YWI4RGgzak5YaVYxUW1NZjZ4ZmZJIiwiaWF0IjoxNjU1MTMwNDAzLCJleHAiOjE2NTUyMTY4MDMsImF1ZCI6ImI1MG5iZnFDanR5N0N5d0RDcHdUOWc5X0hYZFJJNk1fIiwiaXNzIjoiaHR0cHM6Ly9jb3gybS1zYW5kYm94LWltYWdpbmUtYXBpLmNveDJtLmNvbSIsInN1YiI6IjJCdnZNemxnWkdIeDVYU2pDbWNVeHYifQ.jjTVzcdI-mKzAYPOvaBsnBF0tDRppA2G1HzaPTGvgpE"
    }

I got it to work but now when I implement it into my project library and call it I get the below error:

java.lang.InternalError: java.lang.InternalError: java.io.IOException: Unable to establish loopback connection

Here is how I have it set up:
image

Where are you calling from? Script console, or via a component somewhere?
“Unable to establish loopback” is pretty odd…

I am calling it from a component.

Hmm. You might need to contact support so they can take a live look at things; I’m not really sure why you’d be getting that error outside of weird OS restrictions. Does it work (for testing) if you invoke in the script console?