# fetch\_url, need to get the webpage content on urllib2.HTTPError exception.

**URL:** <https://forum.ansible.com/t/fetch-url-need-to-get-the-webpage-content-on-urllib2-httperror-exception/20749>\
**Category:** Ansible Developer\
**Created:** [March 2, 2016, 4:00pm UTC](https://forum.ansible.com/t/fetch-url-need-to-get-the-webpage-content-on-urllib2-httperror-exception/20749 "2016-03-02T16:00:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olivier\_GROSJEANNE](https://avatars.discourse-cdn.com/v4/letter/o/58f4c7/32.png) [@Olivier\_GROSJEANNE](https://forum.ansible.com/u/Olivier_GROSJEANNE)\
**Post date:** [March 2, 2016, 4:00pm UTC](https://forum.ansible.com/t/fetch-url-need-to-get-the-webpage-content-on-urllib2-httperror-exception/20749/1 "2016-03-02T16:00:03Z")

</div>

Hi there,

I wrote modules to manage Dimension Data Cloud Platform. [https://github.com/job-so/ansible.CaaS](https://github.com/job-so/ansible.CaaS)

These modules are working well, and already tested by different users for different use cases.

Before proposing a PR to _ansible-modules-extras_, i am cleaning the documentation and i am checking the community recommendations.  
It includes the replacement of url2lib by fetch\_url

I am facing the following issue : i need to handle the 400-Bad request HTTP error of an underlying API.  
This API is providing detailed information about the error in the web page content.

[Quote from [https://docs.python.org/2/library/urllib2.html](https://docs.python.org/2/library/urllib2.html)]

\*exception\* `urllib2.``HTTPError`

Though being an exception (a subclass of [`URLError`](https://docs.python.org/2/library/urllib2.html#urllib2.URLError)), an [`HTTPError`](https://docs.python.org/2/library/urllib2.html#urllib2.HTTPError) can also function as a non-exceptional file-like return value (the same thing that [`urlopen()`](https://docs.python.org/2/library/urllib2.html#urllib2.urlopen) returns). This is useful when handling exotic HTTP errors, such as requests for authentication.

[/Quote]

So i need to access to e.read() method.

845: except urllib2.HTTPError, e:  
**+++: r = e # \<=== or something smarter**  
846: info.update(dict(msg=str(e), status=e.code, \*\*e.info()))

Do you think we can fix the code in this way ? I may do the fix/PR if needed.

Thank you

Olivier.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [March 2, 2016, 4:20pm UTC](https://forum.ansible.com/t/fetch-url-need-to-get-the-webpage-content-on-urllib2-httperror-exception/20749/2 "2016-03-02T16:20:47Z")

</div>

I wouldn’t have a problem with that approach. That sounds reasonable.

I tested that change with the integration tests, specifically for the `uri` and `get_url` modules, and everything was successful.

---

<div class="post-metadata">

**Author:** ![Olivier\_GROSJEANNE](https://avatars.discourse-cdn.com/v4/letter/o/58f4c7/32.png) [@Olivier\_GROSJEANNE](https://forum.ansible.com/u/Olivier_GROSJEANNE)\
**Post date:** [March 11, 2016, 6:47am UTC](https://forum.ansible.com/t/fetch-url-need-to-get-the-webpage-content-on-urllib2-httperror-exception/20749/3 "2016-03-11T06:47:54Z")

</div>

Thanks Matt,

As there is no other comment, here is the Pull Request :

[https://github.com/ansible/ansible/pull/14915](https://github.com/ansible/ansible/pull/14915)

Thank you

Olivier.
