在Postman中使用原始变量和对象作为方法参数测试端点 [英] Testing an endpoint with a primitive variable and an object as method parameters in Postman

查看:560
本文介绍了在Postman中使用原始变量和对象作为方法参数测试端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用C#编写了一个端点,该端点带有一个原始变量和一个对象。我想通过传递ID以及包含可用于过滤查询并返回用户正在寻找的结果的属性的对象来测试此端点。我面临的问题是,我无法弄清楚如何在Postman中同时传递Id和对象,以便可以测试端点。



我试图将courseId与正文中的对象一起作为常规参数传递,但是它不起作用。我还尝试在正文中传递内容,同时将 [FromBody] 放在CourseId的数据类型之前,它也行不通。



有什么建议吗?



谢谢。



这里是方法我的控制器:

  [HttpGet] 
public List< CourseDAO> Courses(long?courseId,[FromBody] CourseFilter paramsObject)
{
//创建CourseService类的实例
CourseService course = new CourseService();

//返回CourseService类中的GetAllCourses方法的结果
返回course.Courses(courseId,paramsObject);
}

对象内部有诸如活动(布尔),已删除(布尔)之类的属性,等等。例如,这两个属性用于确定是否提供/暂时不提供课程,如果是活动,则永久不提供课程。

解决方案

您的端点是正确的参数方式,您只是在使用错误的HTTP动词。





了解有关路由和HTTP动词的更多信息此处


I have written an endpoint in C# which takes a primitive variable and an object. I want to test this endpoint by passing the Id, along with an object that contains properties that can be used to filter a query and return the result that the user is looking for. The problem that I am facing is that, I cannot figure out how to pass both the Id and object at the same time in Postman so that I can test the endpoint.

I have tried to pass the courseId as regular parameter, along with the object in the body, but it doesn't work. I also tried to pass both in the body and at the same time put the [FromBody] before the datatype of courseId, it also didn't work.

Any suggestions?

Thanks.

Here is the method in my controller:

[HttpGet]
public List<CourseDAO> Courses(long? courseId, [FromBody]CourseFilter paramsObject)
{
    //Create an instance of the CourseService class
    CourseService course = new CourseService();

    //Return the result of the GetAllCourses method in the  CourseService class
    return course.Courses(courseId, paramsObject);
}

Inside the object are properties such as Active (bool), Deleted (bool), etc. These two properties for example are used to determine whether a course is offered/temporarily not offered in the case of Active or permanently not offered in the case of Deleted.

解决方案

Your endpoint is correct parameter-wise, you're just using the wrong HTTP verb.

HttpGet only allows you to supply a query string to the method.

In order to post a body, you need to use HttpPost:

[HttpPost]
public List<CourseDAO> Courses(long? courseId, [FromBody] CourseFilter paramsObject)
{
   // ...
}

You should now be able to correctly access your endpoint using Postman:

Learn more about routing and HTTP verbs here.

这篇关于在Postman中使用原始变量和对象作为方法参数测试端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆