I am trying to create a post request to the tumblr api. Shown below is an extract from said api:
The Write API is a very simple HTTP interface. To create a post, send a POST request to http://www.tumblr.com/api/write with the following parameters:
email - Your account's email address.
password - Your account's password.
type - The post type.
These are the essential elements. I would like to send a photo to the api. According to the API, this is how I would structure my request:
email: myEmail
password: myPassword
type: photo
data: "c:\\img.jpg"
Thanks to dtb, I can send a REGULAR post, which only uses a string to send text, it does not support sending images.
var postData = new NameValueCollection
{
{ "email", email },
{ "password", password },
{ "type", regular },
{ "body", body }
};
using (var client = new WebClient())
{
client.UploadValues("http://www.tumblr.com/api/write", data: data);
}
This works for sending a regular, however according to the API, I should send the image in a multipart/form-data
,
alternatively I could send it in a Normal POST method
,
however, filesizes are not as high as allowd with the former.
client.UploadValues
supports data: which allows me to pass postData
in to it. client.UploadData
also does but I cannot figure out how to use it, I have referred to the documentation.
Also, an opened file cannot be passed in a NameValueCollection which baffles me as to how I could possibly send the request.
Please, if anyone knows the answer I would be extremely grateful if you would help.
I was able to figure this out using RestSharp
library.
//Create a RestClient with the api's url
var restClient = new RestClient("http://tumblr.com/api/write");
//Tell it to send a POST request
var request = new RestRequest(Method.POST);
//Set format and add parameters and files
request.RequestFormat = DataFormat.Json; //I don't know if this line is necessary
request.AddParameter("email", "EMAIL");
request.AddParameter("password", "PASSWORD");
request.AddParameter("type", "photo");
request.AddFile("data", "C:\\Users\\Kevin\\Desktop\\Wallpapers\\1235698997718.jpg");
//Set RestResponse so you can see if you have an error
RestResponse response = restClient.Execute(request);
//MessageBox.Show(response) Perhaps I could wrap this in a try except?
It works, but I'm not sure if it's the best way to do it though.
If anyone has some more suggestions I will gladly take them.