Hello. There were difficulties in interacting with the API . Neither can I make a POST request to send a letter, in response I get 400 ошибку . The trouble is that I can not understand what the problem is, help me figure it out.
To send a letter I use the following code:
var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var content = new StringContent(json, Encoding.UTF8, "application/json"); var url = "https://api.sendpulse.com/smtp/emails/"; var response = client.PostAsync(url, content).Result; // 400 Ошибка Json data:
{ "text" : "Мой текст", "subject" : "Моя тема", "from" : { "name" : "Admin", "email" : "myEmail@yandex.ru" }, "to" : [ { "name" : "Client", "email" : "client1@yandex.ru" } ] } PS
1) The authorization token was received correctly, sent for verification with this token Get all requests OK.
2) Json data is valid, checked them for correctness with the JsonSerializer serializer.
https://api.sendpulse.com/smtp/emails, and you have added a trailing slash to it -https://api.sendpulse.com/smtp/emails/. Maybe with this service it matters. - Mark ShevchenkoJsondata. In fact, I tried the address api.sendpulse.com/smtp/emails backslash and remove and leave, but there was no sense. In general, everyone has already lost his temper)) - sp7htmlrequired field. It is also possiblebccrequired field and must be an empty array. In general, of course, it is more correct to contact the support service by sending them your JSON. - Mark Shevchenkohtmland other attributes, I found out, they are not all mandatory, but used if necessary. Like if thehtmlversion of the letter is sent, then thehtmlused, otherwise justtextsufficient. Although in the course of my experiments, I tried to leave all attributes from the array provided in the documentation, with the exception ofhtmlandattachments, but the result is the same. - sp7ObjectContent:var content = new ObjectContent(typeof(SendParamters), sendParamters, new JsonMediaTypeFormatter()); var result = client.PostAsync(uri, content);var content = new ObjectContent(typeof(SendParamters), sendParamters, new JsonMediaTypeFormatter()); var result = client.PostAsync(uri, content);Here,SendParametersis a C # class that describes JSON attributes. Typepublic class User { public string name { get; set; } public string email { get; set; } } public class SendParameters { public string text { get; set; } public string html { get; set; } public string subject { get; set; } public User from { get; set; } public IEnumerable<User> to { get; set; } ...}public class User { public string name { get; set; } public string email { get; set; } } public class SendParameters { public string text { get; set; } public string html { get; set; } public string subject { get; set; } public User from { get; set; } public IEnumerable<User> to { get; set; } ...}public class User { public string name { get; set; } public string email { get; set; } } public class SendParameters { public string text { get; set; } public string html { get; set; } public string subject { get; set; } public User from { get; set; } public IEnumerable<User> to { get; set; } ...}. Try it. - Mark Shevchenko