我尝试使用C#中的restsharp调用Postmarks电子邮件服务RestAPI并获取" {\" ErrorCode \":402,\"消息\":\"收到无效的JSON输入。\"}"每次我发送它的错误,即使我已经将数据格式设置为json,我甚至尝试过自定义json序列化。我不确定我是否正确设置参数甚至它是实现我尝试的最佳方式,所以任何帮助和建议都会受到欢迎。
public void SendRest(string emailAddress, string subject, string body, params string[] recipients)
{
var recipientString = "";
foreach (var recipient in recipients)
{
recipientString = recipientString + recipient + ',';
}
var client = new RestClient();
client.BaseUrl = new Uri("https://api.postmarkapp.com/email");
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/json");
request.AddHeader("X-Postmark-Server-Token", "Valid-Token");
request.AddParameter("From", "test@testington.co.uk");
request.AddParameter("To", "robin.windon@hotmail.co.uk");
request.AddParameter("Subject", "hi");
request.AddParameter("TextBody", "this is a test");
request.AddParameter("TrackOpens", true);
request.AddParameter("TrackLinks", "None");
IRestResponse response = client.Execute(request);
}
答案 0 :(得分:0)
您将参数作为(序列化)POST参数(在正文中)或GET参数发送,我不确定restsharp如何处理这个。
但是你没有根据需要在POST正文中发送任何JSON。
我想这应该是这样的:
var params = JSON.Stringify(
{
"From": "robin.windon...",
"To": "",
"Subject": "",
"TextBody": "",
"TrackOpens": "",
"TrackLinks": ""
}
);
request.AddParameter("application/json", params, ParameterType.RequestBody);