3

So the process I'm performing seems to make logical sense to me but I keep getting an error. So I have this binary file I'm trying to send to a server (Shapeways to be exact. It's a binary 3d model file) so I go through this process to make it acceptable in a URL

theFile = open(fileloc,'rb')
contents = theFile.read()
b64 = base64.urlsafe_b64encode(contents)
url = urllib.urlencode(b64)  # error

The problem is the last line always throws the error

TypeError: not a valid non-string sequence or mapping object

Which doesn't make sense to me as the data is suppose to be encoded for URLs. Is it possible it simply contains other characters that weren't encoded or something like that?

Jared Joke
  • 1,226
  • 2
  • 18
  • 29
  • Did you try looking at `type(b64)` and then reading the documentation of `urllib.urlencode()` to see what it allows as input? – Phillip Cloud Sep 06 '13 at 21:13
  • Where on [the page you linked](https://developers.shapeways.com/getting-started?li=dh_gs#upload-and-sell) did you see instructions telling you to do this? It's much harder to figure out what you're doing wrong if we first have to guess what you were trying to do. – abarnert Sep 06 '13 at 21:15

2 Answers2

2

urllib.urlencode takes a sequence of two-element tuples or dictionary into a URL query string (it is basicly excerpt from docstring), but you are passing as a argument just a string.

You can try something like that:

theFile = open(fileloc,'rb')
contents = theFile.read()
b64 = base64.urlsafe_b64encode(contents)
url = urllib.urlencode({'shape': b64})

but all you get inside url variable is encoded params, so you still need actual url. If you don't need low level operations it is better to use requests library:

import requests
import base64

url = 'http://example.com'
r = requests.post(
    url=url,
    data={'shape':base64.urlsafe_b64encode(open(fileloc, 'rb').read())}
)
zero323
  • 322,348
  • 103
  • 959
  • 935
  • Well, I'm following the instructions here: https://developers.shapeways.com/getting-started?li=dh_gs#upload-and-sell And I'm not using a simple request but an OAuth1 library called "requests-oauthlib". It is built on requests so it's possible it will already handle the encoding for me, although I'm not sure – Jared Joke Sep 06 '13 at 21:01
  • Actually requests supports OAuth 1 out of the box: http://docs.python-requests.org/en/latest/user/authentication/#oauth-1-authentication – zero323 Sep 06 '13 at 21:11
1

If you're just trying to send a file to your server, you shouldn't need to urlencode it. Send it using a POST request.

You can use urllib2 or you could use the requests lib which can simplify things a bit.

This SO thread may help you as well.

Community
  • 1
  • 1
wallacer
  • 12,855
  • 3
  • 27
  • 45
  • The problem with this suggestion is that it isn't just a POST request. I'm using OAuth1 so I'm using a library for that. It's possible that already encodes the data when it's sent but the spec said to specifically encode the file data. Here: https://developers.shapeways.com/getting-started?li=dh_gs#upload-and-sell – Jared Joke Sep 06 '13 at 21:00