|
Halloo

Update:
What is Atom API?
Atom API is a REST based web service API Blogger used to replace XML-RPC based Blogger API. Usually, a weblog applications (Wordpress, Movable Type, etc) use API to make other program-such as desktop blogging apps- has an ability to communicate with them. So it doesn't always necessary to log in to the web to post, edit or even delete an article in one's blog. Or maybe posting an article from other Websites . There are many weblogging APIs, most of them are XML-RPC based, such as MetaWeblog API, Live Journal API and Blogger API.

Here is my first simple ruby code with Atom API :

require 'net/https'
require 'builder'
xml = ''
#generating XML document
doc = Builder::XmlMarkup.new(:target => xml, :indent =>2)
doc.instruct!
doc.entry(:xmlns=>'http://purl.org/atom/ns#'){|entry|
entry.title("Test Atom API", :mode =>'escaped', :type=>'text/plain')
entry.issued("2006-08-12T06:07:20Z")
entry.generator("DendiPoster", :url =>'http://mociman.blogspot.com')
entry.content(:type =>'application/xhtml+xml'){|content|
content.div("Halloo",:xmlns=>'http://www.w3.org/1999/xhtml')
}
}
#sending HTTP Request
Net::HTTP.version_1_1 #use HTTP version 1.1
http = Net::HTTP.new('www.blogger.com', 443)
http.use_ssl = true
http.start do |http|
request = Net::HTTP::Post.new('/atom/7917465')
request.set_content_type 'application/xml'
request.basic_auth 'user', 'pwd'
request.body=(xml)
response = http.request(request)
puts response.body
puts response.header
end
It uses builder library, so you'll need to gem install builder first to make it work.
This code sends HTTP Post Request to http://www.blogger.com/atom/7917465 (7917465 is this blog's blogID). To Post a new entry in Atom API we send an HTTP Request like this :

POST /atom/3187374 HTTP/1.1
Content-type: application/xml
Host: www.blogger.com
Authorization: BASIC c3RldmVqOm5vdGFyZWFscGFzc3dvcmQ=
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<entry xmlns="http://purl.org/atom/ns#">
<title mode="escaped" type="text/plain">atom test</title>
<issued>2004-04-12T06:07:20Z</issued>
<generator url="http://www.yoursitesurlhere.com">Your client's name here.</generator>
<content type="application/xhtml+xml">
<div xmlns="http://www.w3.org/1999/xhtml">Testing the Atom API</div>
</content>
</entry>
My codes print the 'Halloo' part only , the rest of this article is written from usual Edit Posts page. ;-p


References :

Komentar Terbaru

Tinggalkan Pesan


Name
Email
URI
Msg

Tags

Gegambaran

Loenpia.net