firezhang
firezhang

Reputation: 171

Why I can't use urlencode to encode json format data?

I have a problem about urlencode in python 2.7:

>>> import urllib
>>> import json
>>> urllib.urlencode(json.dumps({'title':"hello world!",'anonymous':False,'needautocategory':True}))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/urllib.py", line 1280, in urlencode
    raise TypeError
TypeError: not a valid non-string sequence or mapping object

Upvotes: 16

Views: 42109

Answers (5)

(1) Import libraries

import requests
import json

(2) Spec is a dictionary object

spec = {...}

(3) Convert dictionary object to json

data = json.dumps(spec, ensure_ascii=False)

(4) Finally, do request with parameter spec in json format

response = requests.get(
    'http://localhost:8080/...',
    params={'spec': data}
)

(5) Analyze response ...

Upvotes: 3

John R Perry
John R Perry

Reputation: 4192

For those of ya'll getting the error:

AttributeError: module 'urllib' has no attribute 'urlencode'

It's because urllib has been split up in Python 3


Here's the code for Python 3:

import urllib.parse

data = {
    "title": "Hello world",
    "anonymous": False,
    "needautocategory": True
}

urllib.parse.urlencode(data)
# 'title=Hello+world&anonymous=False&needautocategory=True'

Upvotes: 3

Trott
Trott

Reputation: 70075

json.dumps() returns a string.

urllib.urlencode() expects a query in the format of a mapping object or tuples. Note that it does not expect a string.

You're passing the first as the parameter for the second, resulting in the error.

Upvotes: 3

ᅠᅠᅠ
ᅠᅠᅠ

Reputation: 66980

urlencode can encode a dict, but not a string. The output of json.dumps is a string.

Depending on what output you want, either don't encode the dict in JSON:

>>> urllib.urlencode({'title':"hello world!",'anonymous':False,'needautocategory':True})
'needautocategory=True&anonymous=False&title=hello+world%EF%BC%81'

or wrap the whole thing in a dict:

>>> urllib.urlencode({'data': json.dumps({'title':"hello world!",'anonymous':False,'needautocategory':True})})
'data=%7B%22needautocategory%22%3A+true%2C+%22anonymous%22%3A+false%2C+%22title%22%3A+%22hello+world%5Cuff01%22%7D'

or use quote_plus() instead (urlencode uses quote_plus for the keys and values):

>>> urllib.quote_plus(json.dumps({'title':"hello world!",'anonymous':False,'needautocategory':True}))
'%7B%22needautocategory%22%3A+true%2C+%22anonymous%22%3A+false%2C+%22title%22%3A+%22hello+world%5Cuff01%22%7D'

Upvotes: 23

DrTyrsa
DrTyrsa

Reputation: 31951

Because urllib.urlencode "converts a mapping object or a sequence of two-element tuples to a “percent-encoded” string...". Your string is neither of these.

I think you need urllib.quote or urllib.quote_plus.

Upvotes: 17

Related Questions