创建对tumblr的POST请求
本文关键字:POST 请求 tumblr 创建 | 更新日期: 2023-09-27 18:28:32
我正在尝试创建一个到tumblrAPI的post请求。下面显示的是所述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.
这些是基本要素。我想把一张照片发给api。根据API,这就是我的请求结构:
email: myEmail
password: myPassword
type: photo
data: "c:''img.jpg"
多亏了dtb,我可以发送一个REGULAR帖子,它只使用字符串发送文本,不支持发送图像。
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);
}
这适用于发送常规,但根据API,我应该在multipart/form-data
中发送图像,
或者我可以在Normal POST method
中发送图像。
然而,文件大小没有前者允许的那么高。client.UploadValues
支持数据:这使我可以将postData
传递给它。client.UploadData
也支持,但我不知道如何使用它,我已经参考了文档
此外,打开的文件无法在NameValueCollection中传递,这让我很困惑如何发送请求
请,如果有人知道答案,如果你能帮忙,我将不胜感激。
您可以使用WebClient
类及其UploadValues
方法执行带有application/x-www-form-urlencoded
有效负载的POST请求:
var data = 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);
}
我能够使用RestSharp
库来解决这个问题。
//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?
它有效,但我不确定这是否是最好的方法。
如果有人有更多的建议,我很乐意接受。