Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With regard to your test script, it is failing because you are passing unicode object to <code>urllib.urlencode()</code> (it is being called for you by <code>urlopen()</code>). It does not support unicode objects, so it implicitly encodes using the default charset, which is <code>ascii</code>. Obviously, it fails.</p> <p>The simplest way to handle POSTing unicode objects is to be explicit; Gather your data and build a dict, encode unicode values with an appropriate charset, urlencode the dict (to get a POSTable ascii string), then initiate the request. Your example could be rewritten as:</p> <pre><code>import urllib import urllib2 ## Build our post data dict data = { 'xdat:user.login' : u'unitest', 'xdat:primary_password' : u'nauihe4r93nf83jshhd83', 'xdat:firstname' : u"abc_\u03a0\u03a3\u03a9", 'xdat:lastname' : u"abc_\u03a0\u03a3\u03a9", } ## Encode the unicode using an appropriate charset data = dict([(key, value.encode('utf8')) for key, value in data.iteritems()]) ## Urlencode it for POSTing data = urllib.urlencode(data) ## Build a POST request, get the response url = "http://localhost:8081/xnat/app/action/XDATRegisterUser" request = urllib2.Request(url, data) response = urllib2.urlopen(request) </code></pre> <p><strong>EDIT:</strong> More generally, when you make an http request with python (say <code>urllib2.urlopen</code>), the content of the response is <em>not</em> decoded to unicode for you. That means you need to be aware of the encoding used by the server that sent it. Look at the <code>content-type</code> header; Usually it includes a <code>charset=xyz</code>.</p> <p>It is always prudent to decode your input as early as possible, and encode your output as late as possible.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload