如何在正文请求中将参数发送到Web API?(How to send parameters to web API in the body request?)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
如何在正文请求中将参数发送到Web API?(How to send parameters to web API in the body request?)

作为集成测试的一部分,我试图从控制台应用程序中使用WebAPI。 我使用这种方法:

private static string Consume(string endpoint, string user, string password) { var client = new HttpClient(); client.BaseAddress = new Uri(endpoint); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); string body = "{\"User\":\"" + user + "\",\"Password\":\"" + password + "\"}"; var response = client.PostAsync("api/ticket", new StringContent(body, Encoding.UTF8, "application/json")).Result; response.EnsureSuccessStatusCode(); if (response.IsSuccessStatusCode) ... //process return string.Empty; }

我对将参数发送到WebAPI方法的方式感到不满意,我更喜欢使用类型化的对象,如:

public class Credentials { public string User { get; set; } public string Password { get; set; } }

并放在体内。 有没有人有这方面的建议?

I am trying to consume a WebAPI from a console application, as part of an integration test. I use this method:

private static string Consume(string endpoint, string user, string password) { var client = new HttpClient(); client.BaseAddress = new Uri(endpoint); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); string body = "{\"User\":\"" + user + "\",\"Password\":\"" + password + "\"}"; var response = client.PostAsync("api/ticket", new StringContent(body, Encoding.UTF8, "application/json")).Result; response.EnsureSuccessStatusCode(); if (response.IsSuccessStatusCode) ... //process return string.Empty; }

I don't feel very satisfied with the way I am sending the parameters to the WebAPI method, I prefer to use a typed object like:

public class Credentials { public string User { get; set; } public string Password { get; set; } }

and put it in the body. Does anyone has a suggestion for this?

最满意答案

使用Newtonsoft.Json库来序列化您的凭证对象。 你的Consume功能就是这样

private static string Consume(string endpoint, string user, string password) { var client = new HttpClient(); client.BaseAddress = new Uri(endpoint); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); var credential = new Credentials { User = user, Password = password }; var credentialString = Newtonsoft.Json.JsonConvert.SerializeObject(credential, Formatting.None); var response = client.PostAsync("api/ticket", credentialString).Result; response.EnsureSuccessStatusCode(); if (response.IsSuccessStatusCode) ... //process return string.Empty; }

credentialString是序列化的Credentials类对象的字符串。

Use Newtonsoft.Json library to serialize your credential object. Your Consume function would be like this

private static string Consume(string endpoint, string user, string password) { var client = new HttpClient(); client.BaseAddress = new Uri(endpoint); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); var credential = new Credentials { User = user, Password = password }; var credentialString = Newtonsoft.Json.JsonConvert.SerializeObject(credential, Formatting.None); var response = client.PostAsync("api/ticket", credentialString).Result; response.EnsureSuccessStatusCode(); if (response.IsSuccessStatusCode) ... //process return string.Empty; }

credentialString is serialize string of Credentials class object.

更多推荐

本文发布于:2023-04-13 12:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/2c1a9e24d4ca9359c64569e23ee8e880.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   中将   参数   如何在   正文

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!