Upload a file to a WordPress blog via XML-RPC wp.uploadFile
This is what the API says:
wp.uploadFile
Upload a file.
Parameters
- int blog_id
- string username
- string password
- struct data
- string name
- string type
- base64 bits
- bool overwrite
Return Values
- struct
- string file
- string url
- string type
Sounds quite easy, doesn’t it? The gotcha I ran into was about the Base64 encoding.
Wrong: XMLRPC with Base64.encode64
...
post_data = {
:name => image.original_filename,
:type => image.content_type,
:bits => Base64.encode64(File.open("#{image.path}").read),
:overwrite => 1
}
...
First, everything seemed to be ok. The answer from the server was charming, but looking into the wordpress media-section: the image had no content. Allright, time for a PacketSniffer – I like EavesDrop.
<methodCall>
<methodName>wp.uploadFile</methodName>
<params>
<param><value><i4>0</i4></value></param>
<param><value><string>username</string></value></param>
<param><value><string>password</string></value></param>
<param><value>
<struct>
<member><name>type</name><value><string>image/jpeg</string></value></member>
<member><name>bits</name><value><string>the Base64 encoded file content</string></value></member>
<member><name>name</name><value><string>image.jpg</string></value></member>
<member><name>overwrite</name><value><i4>1</i4></value></member>
</struct>
</value></param>
</params>
</methodCall>
Yak! There it was. The Base64 encoded section was assigned as a string. Grabbing a bit deeper brought the solution:
Right: XMLRPC with XMLRPC::Base64
...
post_data = {
:name => image.original_filename,
:type => image.content_type,
:bits => XMLRPC::Base64.new(File.open("#{image.path}").read),
:overwrite => 1
}
...
This worked and the PacketSniffer shows why – now the Base64 encoded section is assigned to a <base64> tag:
<methodCall>
<methodName>wp.uploadFile</methodName>
<params>
<param><value><i4>0</i4></value></param>
<param><value><string>username</string></value></param>
<param><value><string>password</string></value></param>
<param><value>
<struct>
<member><name>type</name><value><string>image/jpeg</string></value></member>
<member><name>bits</name><value><base64>the Base64 encoded file content</base64></value></member>
<member><name>name</name><value><string>image.jpg</string></value></member>
<member><name>overwrite</name><value><i4>1</i4></value></member>
</struct>
</value></param>
</params>
</methodCall>
| Print article | This entry was posted by dirk on 20.01.2011 at 17:50, and is filed under Ruby, Wordpress. Follow any responses to this post through RSS 2.0. You can skip to the end and leave a response. Pinging is currently not allowed. |

about 10 months ago
Thanks for the post, just one thing, if you’re on Windows make sure you get the bits like:
:bits => XMLRPC::Base64.new(File.open(“#{image.path}”, “rb”).read)
about 5 months ago
Hiya,
Useful stuff. I was having trouble and my xml tag was wrong. That was fixed easy enough, but I still have the same symptoms – a 0×0 image…
stuck on this one.