Echo API draft 2 is out.

Some people have expressed concern about how difficult it will be to publish using straight XML, rather than an RPC wrapper like XML-RPC or SOAP. Here is a working example (in Python of course, although Mark Hershberger has a similar example in Perl) taken from Section 4.2: Creating a new entry:

from xmltramp import Element, parse
import urllib2

entry = Element('entry')
entry.title = 'My First Entry'
entry.subtitle = 'In which a newbie learns to blog...'
entry.summary = 'Very boring entry...'
entry.author = Element('name')
entry.author.name = 'Bob B. Bobbington'
entry.author.homepage = 'http://bob.name/'
entry.author.weblog = 'http://bob.info/'
entry.issued = '2003-07-07T23:50:00'
entry.content = parse('<div xmlns="http://www.w3.org/1999/xhtml">This is my full content entry.  It <em>contains</em> inline XHTML markup.</div>')
entry.content('type', 'application/xhtml+xml')
entry.content('xml:lang', 'en')

print 'Sending...'
print
print entry.__repr__(1,1)
print

request = urllib2.Request('http://example.org/reilly',
                          entry.__repr__(1,1),
                          {"Content-Type":"application/not-echo+xml"})
reply = urllib2.urlopen(request)
print 'new entry is located at', reply.headers.dict['location']

This prints the following:

Sending...

<entry>
    <title>My First Entry</title>
    <subtitle>In which a newbie learns to blog...</subtitle>
    <summary>Very boring entry...</summary>
    <author>
        <name>Bob B. Bobbington</name>
        <homepage>http://bob.name/</homepage>
        <weblog>http://bob.info/</weblog>
    </author>
    <issued>2003-07-07T23:43:43</issued>
    <content xml:lang='en' type="application/xhtml+xml">
        <div xmlns="http://www.w3.org/1999/xhtml">This is my full content entry.  It <em>contains</em> inline XHTML markup.</div>
    </content>
</entry>

new entry is located at http://example.org/reilly/1

As a comparison, here is what the same transaction might look like with an XML-RPC-based API. This is based on the prototype by Georg Bauer, who explains the mapping is as follows: an element with just text is just mapped to a simple string. An element with attributes and a string is mapped to a structure where the attributes start with _ and namespaces are noted as _namespace_attribute and the value itself is noted as value_. An element with subelements is mapped to a structure with the tags as structure elements. Multiple values are represented as arrays.

import xmlrpclib

entry = {}
entry['title'] = 'My First Entry'
entry['subtitle'] = 'In which a newbie learns to blog...'
entry['summary'] = 'Very boring entry...'
entry['author'] = {}
entry['author']['name'] = 'Bob B. Bobbington'
entry['author']['homepage'] = 'http://bob.name/'
entry['author']['weblog'] = 'http://bob.info/'
entry['issued'] = xmlrpclib.DateTime('2003-07-07T23:50:00')
entry['content'] = {}
entry['content']['value_'] = '<div xmlns="http://www.w3.org/1999/xhtml">This is my full content entry.  It <em>contains</em> inline XHTML markup.</div>'
entry['content']['_type'] = 'application/xhtml+xml'
entry['content']['_xml_lang'] = 'en'
print 'Sending...'
print
# xmlrpclib will print serialization of request and reply when verbose=1
reply = xmlrpclib.Server('http://example.org/reilly/RPC2', verbose=1).newPost(entry)
print 'new entry is located at', reply['location']

Which prints this…

<?xml version='1.0'?>
<methodCall>
    <methodName>newPost</methodName>
    <params>
        <param>
            <value>
                <struct>
                    <member>
                        <name>content</name>
                        <value>
                            <struct>
                                <member>
                                    <name>value_</name>
                                    <value>
                                        <string>&lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;This is my full content entry.  It &lt;em&gt;contains&lt;/em&gt; inline XHTML markup.&lt;/div&gt;</string>
                                    </value>
                                </member>
                                <member>
                                    <name>_type</name>
                                    <value>
                                        <string>application/xhtml+xml</string>
                                    </value>
                                </member>
                                <member>
                                    <name>_xml_lang</name>
                                    <value>
                                        <string>en</string>
                                    </value>
                                </member>
                            </struct>
                        </value>
                    </member>
                    <member>
                        <name>subtitle</name>
                        <value>
                            <string>In which a newbie learns to blog...</string>
                        </value>
                    </member>
                    <member>
                        <name>author</name>
                        <value>
                            <struct>
                                <member>
                                    <name>weblog</name>
                                    <value>
                                        <string>http://bob.info/</string>
                                    </value>
                                </member>
                                <member>
                                    <name>homepage</name>
                                    <value>
                                        <string>http://bob.name/</string>
                                    </value>
                                </member>
                                <member>
                                    <name>name</name>
                                    <value>
                                        <string>Bob B. Bobbington</string>
                                    </value>
                                </member>
                            </struct>
                        </value>
                    </member>
                    <member>
                        <name>issued</name>
                        <value>
                            <dateTime.iso8601>2003-07-07T23:50:00</dateTime.iso8601>
                        </value>
                    </member>
                    <member>
                        <name>title</name>
                        <value>
                            <string>My First Entry</string>
                        </value>
                    </member>
                    <member>
                        <name>summary</name>
                        <value>
                            <string>Very boring entry...</string>
                        </value>
                    </member>
                </struct>
            </value>
        </param>
    </params>
</methodCall>

[ ... serialization of response omitted for brevity ... ]

new entry is located at http://example.org/reilly/1

No comments; if you have something to say, say it on the wiki. XmlRpc, XmlRpcDiscussion, SampleXmlrpc, RestAndRpc.

Update: added required xml:lang attribute.

Update 2: removed extraneous print statement in XML-RPC example and added a comment to make it clear where the XML-RPC serialization comes from (xmlrpclib.py prints it when you specify verbose=1).

Update 3: linked to MAH’s Perl example.

§

Ten comments here (latest comments)

  1. techno weenie (trackback)
  2. Confessions of a G33k (trackback)
  3. Noch'n Blogg. (trackback)
  4. Third Superpower (trackback)
  5. Raw Blog (trackback)
  6. Dichotomy's Purgatory (trackback)
  7. Sam Ruby (trackback)
  8. Dichotomy's Purgatory (trackback)
  9. Big Damn Heroes (Tech) (trackback)
  10. OpenWeblog.info (trackback)

Respond privately

I am no longer accepting public comments on this post, but you can use this form to contact me privately. (Your message will not be published.)



§

firehosecodemusicplanet

© 2001–8 Mark Pilgrim