# system.net.httpPost error codes

**URL:** <https://forum.inductiveautomation.com/t/system-net-httppost-error-codes/9699>\
**Category:** Ignition\
**Created:** [February 12, 2015, 2:54pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-error-codes/9699 "2015-02-12T14:54:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HCIdivision17](https://avatars.discourse-cdn.com/v4/letter/h/65b543/32.png) [@HCIdivision17](https://forum.inductiveautomation.com/u/HCIdivision17)\
**Post date:** [February 12, 2015, 2:54pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-error-codes/9699/1 "2015-02-12T14:54:00Z")

</div>

Would it be idiomatic to read error codes from the system.net.httpPost IOError exception, or is there a way to retrieve the HTTP status code?

As an example:

[code]from [system.net](http://system.net) import httpPost

acct = u’username’  
auth = u’password’  
url = ‘[https://www.google.com](https://www.google.com)’

try:  
resp = httpPost(url, username=acct, password=auth)  
print resp  
except IOError, e:  
print e[/code]  
yields

```auto
Server returned HTTP response code: 405 for URL: https://www.google.com
```

I can take the output e and parse it (or sledge it with a regex), but before I start messing with strings, I figured I’d check if I just missed an obvious way to just extract the HTTP status code. Thanks!

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [February 12, 2015, 5:41pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-error-codes/9699/2 "2015-02-12T17:41:33Z")

</div>

No obvious way to extract just the HTTP status code. Hammer away.

Best,

---

<div class="post-metadata">

**Author:** ![HCIdivision17](https://avatars.discourse-cdn.com/v4/letter/h/65b543/32.png) [@HCIdivision17](https://forum.inductiveautomation.com/u/HCIdivision17)\
**Post date:** [February 12, 2015, 8:22pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-error-codes/9699/3 "2015-02-12T20:22:34Z")

</div>

Well, [you know what they say…](http://regex.info/blog/2006-09-15/247)

Here’s a playground script of what I was thinking:

[code]import re

pattern = ‘HTTP response code: ([0-9]{3}) for URL: (http[s]?://.\*)’  
source = ‘Server returned HTTP response code: 405 for URL: [https://www.google.com](https://www.google.com)’

regex = re.compile(pattern)

match = regex.findall(source)

for m in match:  
print m[/code]  
yielding

```auto
('405', 'https://www.google.com')
```

It’ll work, but I can’t help but feel it’s a bit heavy-handed for the job. I could just do some string splicing, but I don’t know if I should count on the string being perfectly identically formatted for all errors.

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [February 12, 2015, 9:06pm UTC](https://forum.inductiveautomation.com/t/system-net-httppost-error-codes/9699/4 "2015-02-12T21:06:04Z")

</div>

Yes. Another approach is to use the builtin Python library module for making http requests: [urllib2](https://docs.python.org/2/howto/urllib2.html)

Or use Java’s [HttpURLConnection](http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html) class.

Remember you have Python and Java libraries available to you in Python scripts.

Best,

---

<div class="post-metadata">

**Author:** ![HCIdivision17](https://avatars.discourse-cdn.com/v4/letter/h/65b543/32.png) [@HCIdivision17](https://forum.inductiveautomation.com/u/HCIdivision17)\
**Post date:** [February 13, 2015, 2:05am UTC](https://forum.inductiveautomation.com/t/system-net-httppost-error-codes/9699/5 "2015-02-13T02:05:28Z")

</div>

Does one approach have any significant advantages?

Getting SSL to work right without calling upon Java is hard (possible in Jython 2.5.3?), so I figured it would be more reliable to use the built-in function. I also guessed there may be some nice side effects, like Ignition handling some connection pooling or the like. And it’s somewhat turn-key (except for error handling, which is going to be non-optimal anyway).

But since I’ll be making quite a few HTTP requests regularly, it wouldn’t be a terrible idea to optimize this early.
