测试接受JSON作为输入的Web API [英] Testing the web api that accepts JSON as input
问题描述
我正在创建一个Web api,它需要接受JSON作为被调用的客户端应用程序发送的输入.下面是代码
公共类SetNameController:ApiController{[HttpPost]公共异步Task< IHttpActionResult>Get([FromBody] CodeDTO.RootObject bCode){字符串Bar_Code = bCode.Barcode.ToString();如果(Bar_Code ==" || Bar_Code == null){....返回确定(响应)}
在这里我不确定客户端应用程序如何调用此Web API,URL可以像
然后,将Body中的有效负载json数据作为原始数据提供:
此外,更改操作名称 Get
可能会引起混淆.如果仍然无法使用api,则可以通过使用 [Route('yourURL')]
属性装饰动作并在邮递员中进行相应更改来使用路由网址.
I am creating a web api that needs to accept JSON as input that is sent by the called client side application. Below is the Code
public class SetNameController : ApiController
{
[HttpPost]
public async Task<IHttpActionResult> Get([FromBody] CodeDTO.RootObject bCode)
{
string Bar_Code = bCode.Barcode.ToString();
if (Bar_Code == "" || Bar_Code == null)
{
....
return OK(response)
}
Here I am not sure How the client application call this Web API, the URL can be just like http://localhost:41594/api/SetName can it accept the JSON? Also can I test this using PostMan or Fiddler?
Specify the following:
- Method: POST
- Header: Content Type
Then, provide the payload json data in Body as raw:
Also, change the name of the action Get
which might cause confusion. If you still cannot hit your api, you can use route urls by decorating the action with [Route('yourURL')]
attribute and change that accordingly in postman.
这篇关于测试接受JSON作为输入的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!