Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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>

 

 

Are you using Bitbucket to accomplish your company's mission? Share your company’s mission with #Forthecode for a chance to be featured on our homepage, our social media channels, or win a free t-shirt!

Topics:

Published at DZone with permission of Chad Lung, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}