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?

2
  • Did you try looking at type(b64) and then reading the documentation of urllib.urlencode() to see what it allows as input? Commented Sep 6, 2013 at 21:13
  • Where on the page you linked 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. Commented Sep 6, 2013 at 21:15

2 Answers 2

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())}
)
Sign up to request clarification or add additional context in comments.

2 Comments

Well, I'm following the instructions here: developers.shapeways.com/… 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
Actually requests supports OAuth 1 out of the box: docs.python-requests.org/en/latest/user/authentication/…
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.

1 Comment

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: developers.shapeways.com/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.