[英]C# Send data with method POST to API
不知道我做错了什么,需要用我的 VSTO Outlook 插件中的数据填充数据库。
jObjectbody.Add( new { mail_from = FromEmailAddress });
mail_from
是数据库中列的名称, FromEmailAddress
是来自我的 Outlook FromEmailAddress
的值
如何正确发送到 API https://my.address.com/insertData
?
RestClient restClient = new RestClient("https://my.address.com/");
JObject jObjectbody = new JObject();
jObjectbody.Add( new { mail_from = FromEmailAddress });
RestRequest restRequest = new RestRequest("insertData", Method.POST);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddParameter("text/html", jObjectbody, ParameterType.RequestBody);
IRestResponse restResponse = restClient.Execute(restRequest);
错误: Could not determine JSON object type for type <>f__AnonymousType0`1[System.String].
如果我在 Postman (POST->Body->raw->JSON) 中尝试此操作,则数据存储在数据库中,除了不要仅使用数据value
。
{
"mail_from":"email@email.com"
}
感谢任何线索取得成功
您可以使用restRequest.AddJsonBody(jObjectbody);
而不是AddParameter
(我相信它会添加一个查询字符串)。
请参阅RestSharp AddJsonBody文档。 他们还提到不要使用某种JObject
因为它不起作用,因此您可能还需要更新您的类型。
以下可能对您有用:
RestClient restClient = new RestClient("https://my.address.com/");
var body = new { mail_from = "email@me.com" };
RestRequest restRequest = new RestRequest("insertData", Method.POST);
restRequest.AddJsonBody(body);
IRestResponse restResponse = restClient.Execute(restRequest);
// extra points for calling async overload instead
//var asyncResponse = await restClient.ExecuteTaskAsync(restRequest);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.