WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

XML
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Post XML File Via HTTP


Question:

I'm on a mission to send posts to a Blogger.com blog using their API through WinBatch, and I'm very stuck at the moment -- hoping someone might be kind enough to lend a hand.

Calls to the API are done via XML-RPC, and they give examples of the XML-RPC calls to use for each function, and the URI they should 'go to' (http://plant.blogger.com/api/RPC2).

Here is an example of a function call they give -- you can see the page at http://www.blogger.com/developers/api/1_docs/xmlrpc_newPost.html :-

<?xml Version="1.0"?>
<methodCall>
<methodName>blogger.newPost</methodName>
<params>
<param><value><STRING>C6CE3FFB3174106584CBB250C0B0519BF4E294</STRING></value></param>
<param><value><STRING>744145</STRING></value></param>
<param><value><STRING>ewilliams</STRING></value></param>
<param><value><STRING>secret</STRING></value></param>
<param><value><STRING>Today I had a peanut butter and pickle sandwich
For lunch. Do you like peanut-butter and pickle sandwiches? I do.
They're yummy. Please comment!</STRING></value></param>
<param><value><boolean>FALSE</boolean></value></param>
</params>
</methodCall>
I'm assuming I have to construct a string containing the above function call and 'send' it to that URI.

There are two things I'm confused about.

  1. Whether or not I have to include the 'header' section as in their example:
    POST /api/RPC2 HTTP/1.0
    User-Agent: Java.Net Wa-Wa 2.0
    Host: plant.blogger.com
    Content-Type: text/xml
    Content-length: 515
    
    ...which is the bit before the XML tags start. OR if this data should be put in whatever WinBatch function I have to use to make this work.
    

  2. How I would go about 'sending' this to the URI they provide, using WinBatch?
(I'm not worried about the parameters within the XML, I think I've got those sorted okay.)

I'm sure there must be someone reading this who knows exactly what I should be doing here! Any help would be very much appreciated.

Answer:

Looks to me like you get the XML text together (without the headers that we'll do for you) then using the WinInet extender, POST it to the URI given. Looks like straight http protocol. Seems like there will be a few gotcha's, like finding where to set the contenttype tag, but I think it should more or less work. Or use MSXML.
; okay I got this to send XML and get back a response, using
;   MSXML that comes with MSIE 5+ and Winbatch.

;   I just don't have the credentials and am new to using XMLHTTP...

xfile = "C:\Jay\xml\Blogger_New_Post.xml"  ;<-- cut & paste the sample XML they supplied...
xmldoc = ObjectOpen("Microsoft.XMLDOM")
xmldoc.async = @FALSE
xmldoc.load(xfile)

xmlhttp = ObjectOpen("Msxml2.XMLHTTP.3.0")
xmlhttp.Open("POST", "http://plant.blogger.com/api/RPC2", @FALSE, "userid", "password")
xmlhttp.Send(xmldoc)

response = xmlhttp.responseXML.xml
ClipPut(response)
Message("Debug", response)

Exit

;   and the response XML...
;
;<?xml version="1.0"?>
;<methodResponse><fault><value><struct><member><name>faultString
;</name><value>com.pyra.blogger.api.UserNotAuthorizedException: We're sorry but the
;Username/Password combination you've entered is either invalid or you don't have
;permission to access this Blog.
;</value>
;</member><member><name>faultCode
;</name><value><int>0
;</int>
;</value>
;</member>
;</struct>
;</value>
;</fault>
;</methodResponse>

User Reply:

Many thanks your valuable input. I have it working now, using the Wininet suggestion as it happens, but the MSXML alternative way is very neat too, and I'll be keeping that excellent example in mind for the future.

Here is the code that works:


#DefineSubRoutine PostToBlogger(blogID,userName,password,content)
AddExtender("WWINT34I.DLL") ;WinInet

host= "www.blogger.com"
mainurl= "/api"
xmlfile= "E:\NAPS\Blog&Ping\xml.txt"
rsltfile= "E:\NAPS\Blog&Ping\PostToBlogger.txt"

xml=FileGet(xmlfile)

appkey="put your app key here!"

;Replace my tags here
xml=StrReplace(xml,"{{APPKEY}}",appkey)
xml=StrReplace(xml,"{{BLOGID}}",blogID)
xml=StrReplace(xml,"{{USERNAME}}",username)
xml=StrReplace(xml,"{{PASSWORD}}",password)
xml=StrReplace(xml,"{{CONTENT}}",content)

tophandle=iBegin(0,"","")
connecthandle=iHostConnect(tophandle, host, @HTTP, "", "")
datahandle=iHttpInit(connecthandle,"POST",mainurl,"", 0)
rslt=iHttpOpen(datahandle, 0, xml, -1);

;Error? Display HTTP headers
If rslt!=200
headers=iHttpHeaders(datahandle)
AskItemlist("Rslt=%rslt%",headers,@TAB,@UNSORTED,@SINGLE)
EndIf

xx=iReadData(datahandle,rsltfile)

iClose(datahandle)
iClose(connecthandle)
iClose(tophandle)
#EndSubRoutine
And the contents of my XML file (angled open brackets changed):-
<?xml Version="1.0"?>
<methodCall>
<methodName>blogger.newPost</methodName>
<params>
<param><value><STRING>{{APPKEY}}</STRING></value></param>
<param><value><STRING>{{BLOGID}}</STRING></value></param>
<param><value><STRING>{{USERNAME}}</STRING></value></param>
<param><value><STRING>{{PASSWORD}}</STRING></value></param>
<param><value><STRING>{{CONTENT}}</STRING></value></param>
<param><value><boolean>1</boolean></value></param>
</params>
</methodCall>
What ISN'T working like a charm is my effort to automate an HTTP POST request to http://rpc.pingomatic.com/ so that I can ping my blog entry from within my program as well. (Actually, I've just posted a new thread asking for help!) I believe the HTTP headers that Winbatch sends out are disliked by the receiving server. Actually, I tried with your alternative methods of posting, and the headers were still wrong for what I believe the receiving server needs. :(

Interestingly, posting to the blog URL was fine, even though the headers were different to what the XML-RPC specification says they should be - perhaps Blogger.com's RPC server is lenient on the headers.


Article ID:   W17342
File Created: 2007:07:03:14:29:32
Last Updated: 2007:07:03:14:29:32