【问题标题】:Fiddler testing API Post passing a [Frombody] classFiddler 测试 API Post 传递 [Frombody] 类
【发布时间】:2013-10-16 05:09:11
【问题描述】:

我有一个名为“TestController”的非常简单的 C# APIController,其 API 方法为:

[HttpPost]
public string HelloWorld([FromBody] Testing t)
{
    return t.Name + " " + t.LastName;
}

Contact 只是一个看起来像这样的类:

public class Testing
{
    [Required]
    public string Name;
    [Required]
    public string LastName;
}

我的 APIRouter 如下所示:

config.Routes.MapHttpRoute(
     name: "TestApi",
     routeTemplate: "api/{controller}/{action}/{id}",
     defaults: new { id = RouteParameter.Optional }
);

问题 1
如何从 C# 客户端进行测试?

对于#2,我尝试了以下代码:

private async Task TestAPI()
{
    var pairs = new List<KeyValuePair<string, string>> 
    {
       new KeyValuePair<string, string>("Name", "Happy"),
       new KeyValuePair<string, string>("LastName", "Developer")
    };

    var content = new FormUrlEncodedContent(pairs);

        var client = new HttpClient();                        
        client.DefaultRequestHeaders.Accept.Add(
             new MediaTypeWithQualityHeaderValue("application/json"));

        var result = await client.PostAsync( 
             new Uri("http://localhost:3471/api/test/helloworld", 
                    UriKind.Absolute), content);

        lblTestAPI.Text = result.ToString();
    }

问题 2
如何从 Fiddler 进行测试?
似乎找不到如何通过 UI 传递类。

【问题讨论】:

    标签: c# asp.net-web-api fiddler


    【解决方案1】:

    对于问题 1: 我将从 .NET 客户端实现 POST,如下所示。 请注意,您需要添加对以下程序集的引用: a) System.Net.Http b) System.Net.Http.Formatting

    public static void Post(Testing testing)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:3471/");
    
            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
    
            // Create the JSON formatter.
            MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
    
            // Use the JSON formatter to create the content of the request body.
            HttpContent content = new ObjectContent<Testing>(testing, jsonFormatter);
    
            // Send the request.
            var resp = client.PostAsync("api/test/helloworld", content).Result;
    
        }
    

    我还要重写控制器方法如下:

    [HttpPost]
    public string HelloWorld(Testing t)  //NOTE: You don't need [FromBody] here
    {
      return t.Name + " " + t.LastName;
    }
    

    对于问题 2: 在 Fiddler 中,将 Dropdown 中的动词从 GET 更改为 POST 并放入 JSON 请求正文中对象的表示

    【讨论】:

    • 不幸的是,如果我删除 [FromBody],Fiddler 不会找到它并得到 404。C# 客户端代码不会得到任何响应。
    • [FromBody] 在传递 Content-Type 时需要:application/json。如果你删除它,你需要传递一个 Content-Type: application/x-www-form-urlencoded
    • 您是否有理由使用 URL 编码的内容,而不是使用“Content-type:application.json”,它可以在正文中指定有效负载?对于复杂类型,您希望使用正文而不是查询字符串。您的控制器中是否定义了多个 POST 操作?你能发布完整的控制器代码吗?
    • 没有真正的原因。主要是我在学习 API 和测试各种东西。当然,Json 是大型机构的方式。我只是想让它在 Fiddler 上工作(现在可以了)。感谢您的意见!
    • 我刚刚完成了一个入站 Twilio 帖子的工作。在 Fiddler 中,我将 Content-Type: application/x-www-form-urlencoded 添加到标题中,然后在正文中添加以下内容: Sid=replace+with+original+sid+for+status+callback&AccountSid=assigned+by+twilio&From =%2B12223334444&To=%2B15556667777&Body=something+really+witty&Status=sent 参数名称与类中的字段名称相同。
    猜你喜欢
    • 2017-08-04
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多