Performance Zone is brought to you in partnership with:

I've been in the world of software development for 14+ years working with most mainstream programming languages at some point. Chad is a DZone MVB and is not an employee of DZone and has posted 38 posts at DZone. You can read more from them at their website. View Full User Profile

Posting a New ATOM Entry to Atom Hopper Using Node.js

05.18.2012
| 3975 views |
  • submit to reddit

Previously I blogged about how you can use Apache Abdera to post ATOM entries to an AtomPub server like Atom Hopper. Today I’ll show you how to do something similar but with Node.js.

Note: The code below is a simplistic example and would need modifications to become production quality. This code can be used for testing purposes, etc.

The Node.js documentation has some sample code to perform an HTTP POST which is exactly what we need to do to post to Atom Hopper. The sample code to POST to Atom Hopper (or any AtomPub server) looks like the following:

var http = require('http');
 
var data = '<entry xmlns="http://www.w3.org/2005/Atom"><title type="text">Good Evening</title>'
    + '<author><name>Chad</name></author><content type="text">This is the content</content></entry>';
 
var headers = {
    'Content-Type': 'application/atom+xml',
    'Content-Length': Buffer.byteLength(data, 'utf8')
};
 
var opts = {
    host: 'localhost',
    port: 8080,
    method: 'POST',
    path: '/atomhopper-1.0.2-SNAPSHOT/namespace/feed/',
    headers: headers
};
 
var req = http.request(opts, function (response) {
    var returnedData = '';
    response.on('data', function (chunk) {
        returnedData += chunk;
    });
    response.on('end', function () {
        console.log('Returned Data: ' + returnedData);
    });
});
 
req.on('error', function (e) {
    console.log("Got Error: " + e.message);
});
 
if(opts.method === 'POST') {
    req.write(data);
}
 
req.end()

The result will be similar to this:

Returned Data:
<entry xmlns="http://www.w3.org/2005/Atom">
  <title type="text">Good Evening</title>
  <author>
    <name>Chad</name>
  </author>
  <content type="text">This is the content</content>
  <id>urn:uuid:e36ef071-65aa-49fb-87c0-6c0219211f0f</id>
  <link href="http://localhost:8080/atomhopper-1.0.2-SNAPSHOT/namespace/feed/entries/urn:uuid:e36ef071-65aa-49fb-87c0-6c0219211f0f" rel="self" />
  <updated>2012-05-01T22:38:50.886Z</updated>
</entry>

 

 

Published at DZone with permission of Chad Lung, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)