2

I'm stuck with example from http://documentation.mailgun.com/user_manual.html?highlight=inline%20image#sending-via-api

UPD: Problem below occurs on virtual machine, while it works on another.

import requests
requests.post(
    "https://api.mailgun.net/v2/samples.mailgun.org/messages",
    auth=("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"),
    files=[("inline", open("files/test.jpg"))],
    data={"from": "Excited User <me@samples.mailgun.org>",
          "to": "bar@example.com",
          "subject": "Hello",
          "text": "Testing some Mailgun awesomness!",
          "html": '<html>Inline image here: <img src="cid:test.jpg"></html>'})

I don't change a line, but it doesn't work, spitting traceback.

NB: don't expect mail sent, but request must occur itself!

Traceback (most recent call last):
  File "test.py", line 34, in <module>
    "html": '<html>Inline image here: <img src="cid:test.jpg"></html>'})
  File "<projectpath>/lib/python2.7/site-packages/requests/api.py", line 88, in post
    return request('post', url, data=data, **kwargs)
  File "<projectpath>/lib/python2.7/site-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "<projectpath>/lib/python2.7/site-packages/requests/sessions.py", line 456, in request
    resp = self.send(prep, **send_kwargs)
  File "<projectpath>/lib/python2.7/site-packages/requests/sessions.py", line 559, in send
    r = adapter.send(request, **kwargs)
  File "<projectpath>/lib/python2.7/site-packages/requests/adapters.py", line 327, in send
    timeout=timeout
  File "<projectpath>/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 498, in urlopen
    body=body, headers=headers)
  File "<projectpath>/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 296, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib64/python2.7/httplib.py", line 946, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python2.7/httplib.py", line 987, in _send_request
    self.endheaders(body)
  File "/usr/lib64/python2.7/httplib.py", line 940, in endheaders
    self._send_output(message_body)
  File "/usr/lib64/python2.7/httplib.py", line 801, in _send_output
    msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 677: ordinal not in range(128)

Some exploration with pdb shows, that when conn.request(method, url, **httplib_request_kw) is executed httplib_request_kw['body'] contains string, that cannot be decoded to unicode correctly, so body is malformed.

curl example works perfectly, but I'd prefer using requests surely.

PS: Sending Mailgun Inline Images in HTML using Python Requests library relates, but not working for me.

Community
  • 1
  • 1
I S
  • 407
  • 1
  • 4
  • 14
  • 1
    looks like you got some unicode bytes in your string. perhaps `u'Inline image here: '` – nsfyn55 Jun 05 '14 at 17:13
  • @nsfyn55 No, it doesn't help. As mentioned in upd, it worked on another computer without any modification. Now trying to figure out crucial difference. – I S Jun 05 '14 at 17:18
  • Did you cut and paste that string? Try typing it out on the target machine character by character. – nsfyn55 Jun 05 '14 at 18:11
  • Thanks for your time, it was totally different issue. – I S Jun 05 '14 at 18:27
  • possible duplicate of [Making HTTP requests via Python Requests module not working via proxy where curl does? Why?](http://stackoverflow.com/questions/8482896/making-http-requests-via-python-requests-module-not-working-via-proxy-where-curl) – I S Jun 20 '14 at 05:55

1 Answers1

0

The problem was outdated httplib.

If you get this issue - mind getting fresh httplib. (Copying to lib/python2.7 seems be enough.) My sys.version is '2.7 (r27:82500, Aug 07 2010, 16:54:59) [GCC]' and it is too old for python-requests 2.3.0.

Possibly solves Uploading file with python-requests - UnicodeDecodeError

Community
  • 1
  • 1
I S
  • 407
  • 1
  • 4
  • 14