在 Postman 中使用原始变量和对象作为方法参数测试端点

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

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

问题描述

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

我尝试将 courseId 作为常规参数以及正文中的对象传递,但它不起作用.我也尝试在body中传递,同时将[FromBody]放在courseId的数据类型之前,它也没有用.

有什么建议吗?

谢谢.

这是我的控制器中的方法:

[HttpGet]公开列表<CourseDAO>课程(长? courseId,[FromBody]CourseFilter paramsObject){//创建CourseService类的实例CourseService course = new CourseService();//返回CourseService类中GetAllCourses方法的结果返回 course.Courses(courseId, paramsObject);}

对象内部是 Active (bool)、Deleted (bool) 等属性.例如,这两个属性用于确定在 Active 的情况下是否提供/暂时不提供课程或永久不提供课程Deleted 的情况.

解决方案

你的端点在参数方面是正确的,你只是使用了错误的 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 中使用原始变量和对象作为方法参数测试端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Postman 中使用原始变量和对象作为方法参数测试端点

基础教程推荐