提琴手测试API邮政传递[Frombody]类 [英] Fiddler testing API Post passing a [Frombody] class

查看:167
本文介绍了提琴手测试API邮政传递[Frombody]类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为的TestController与API方法这个非常简单的C#APIController:

I have this very simple C# APIController named "TestController" with an API method as:

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

联系仅仅是一个类,它是这样的:

Contact is just a class that look like this:

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

我APIRouter看起来是这样的:

My APIRouter looks like this:

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

问题1 :结果
如何测试,从C#的客户端?

QUESTION 1:
How can I test that from a C# Client?

有关#2我尝试以下code:

For #2 I tried the following code:

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 :结果
如何从提琴手测试这个?结果
似乎无法找到如何通过UI传递类。

QUESTION 2:
How can I test this from Fiddler?
Can't seem to find how to pass a Class via the UI.

推荐答案

有关问题1:
我想从.NET客户端实现POST如下。
请注意,您将需要添加参照以下组件:
一)System.Net.Http B)System.Net.Http.Formatting

For Question 1: I'd implement the POST from the .NET client as follows. Note you will need to add reference to the following assemblies: 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;

    }

我还改写控制器方法如下:

I'd also rewrite the controller method as follows:

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

有关问题2:
在提琴手改变动词从GET张贴下拉列表,并把在JSON
重新在请求主体对象的presentation

For Question 2: In Fiddler change the verb in the Dropdown from GET to POST and put in the JSON representation of the object in the Request body

这篇关于提琴手测试API邮政传递[Frombody]类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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