# Importing httplib Library

**URL:** <https://forum.inductiveautomation.com/t/importing-httplib-library/60697>\
**Category:** Ignition\
**Created:** [June 13, 2022, 4:23pm UTC](https://forum.inductiveautomation.com/t/importing-httplib-library/60697 "2022-06-13T16:23:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lauren\_Hart](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@Lauren\_Hart](https://forum.inductiveautomation.com/u/Lauren_Hart)\
**Post date:** [June 13, 2022, 4:23pm UTC](https://forum.inductiveautomation.com/t/importing-httplib-library/60697/1 "2022-06-13T16:23:04Z")

</div>

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

 ![MicrosoftTeams-image (3)](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/e/6ebf3408c09f99e83a26c4bb4fa96d7b36f3805f.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 13, 2022, 4:27pm UTC](https://forum.inductiveautomation.com/t/importing-httplib-library/60697/2 "2022-06-13T16:27:22Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/5/b5a2b13c8a795f9466867b38a283058fe51433f8.png)

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.

---

<div class="post-metadata">

**Author:** ![Lauren\_Hart](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@Lauren\_Hart](https://forum.inductiveautomation.com/u/Lauren_Hart)\
**Post date:** [June 13, 2022, 4:55pm UTC](https://forum.inductiveautomation.com/t/importing-httplib-library/60697/3 "2022-06-13T16:55:06Z")

</div>

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

```auto
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:

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 13, 2022, 5:02pm UTC](https://forum.inductiveautomation.com/t/importing-httplib-library/60697/4 "2022-06-13T17:02:18Z")

</div>

`httpClient` will handle JSON negotiation and encoding for you:

```python
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"]

```

---

<div class="post-metadata">

**Author:** ![Lauren\_Hart](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@Lauren\_Hart](https://forum.inductiveautomation.com/u/Lauren_Hart)\
**Post date:** [June 13, 2022, 5:25pm UTC](https://forum.inductiveautomation.com/t/importing-httplib-library/60697/5 "2022-06-13T17:25:00Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/d/1dcacc6c2254824b573a13bbda17b78806d9419d.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 13, 2022, 5:33pm UTC](https://forum.inductiveautomation.com/t/importing-httplib-library/60697/6 "2022-06-13T17:33:09Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Lauren\_Hart](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@Lauren\_Hart](https://forum.inductiveautomation.com/u/Lauren_Hart)\
**Post date:** [June 13, 2022, 6:01pm UTC](https://forum.inductiveautomation.com/t/importing-httplib-library/60697/7 "2022-06-13T18:01:06Z")

</div>

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

```auto
>>> 
<!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:

```auto
"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"
    }

```

---

<div class="post-metadata">

**Author:** ![Lauren\_Hart](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@Lauren\_Hart](https://forum.inductiveautomation.com/u/Lauren_Hart)\
**Post date:** [June 13, 2022, 8:25pm UTC](https://forum.inductiveautomation.com/t/importing-httplib-library/60697/8 "2022-06-13T20:25:34Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/c/7c6d746445e34483e854893b36893f7a67dee789.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 13, 2022, 8:31pm UTC](https://forum.inductiveautomation.com/t/importing-httplib-library/60697/9 "2022-06-13T20:31:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lauren\_Hart](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@Lauren\_Hart](https://forum.inductiveautomation.com/u/Lauren_Hart)\
**Post date:** [June 14, 2022, 12:23am UTC](https://forum.inductiveautomation.com/t/importing-httplib-library/60697/10 "2022-06-14T00:23:54Z")

</div>

I am calling it from a component.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 14, 2022, 4:51pm UTC](https://forum.inductiveautomation.com/t/importing-httplib-library/60697/11 "2022-06-14T16:51:28Z")

</div>

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?
