如何序列化我的类以通过 refit 将其发送为 urlencoded

Her*_*erb 3 c# serialization json xamarin.forms refit

你好,我正在尝试使用 postman 发送一个经过改装的 POST 方法,到目前为止我可以说它正在工作,如果我使用 x-www-form-encoded 选项发送数据,我发送的 json 看起来像这样

{
  "apt": "APT",
  "apartment": "A103",
  "author": "Someone",
  "is_public": "True",
  "is_complaint": "True",
  "show_name": "True",
  "title": "fhj",
  "details": "vvkko"
}
Run Code Online (Sandbox Code Playgroud)

我在 Visual Studio 中构建了我的类,并构建了我的模型来匹配它,并将其粘贴到 json

namespace App.Models
{
    public class ManyComplaints
    {
        public SingleComplaint data { get; set; }
    }
    public class SingleComplaint
    {
        public string apt { get; set; }
        public string apartment { get; set; }
        public string author { get; set; }
        public string is_public { get; set; }
        public string is_complaint { get; set; }
        public string show_name { get; set; }
        public string title { get; set; }
        public string details { get; set; }
    }

}
Run Code Online (Sandbox Code Playgroud)

在这里我不确定我是否做对了这是我的 api 调用者

 [Headers("Content-Type: application/x-www-form-urlencoded")]
 [Post("/api/complaints/")]
 Task SubmitComplaint([Body(BodySerializationMethod.UrlEncoded)]SingleComplaint complaint);
Run Code Online (Sandbox Code Playgroud)

这是发送数据的代码

public async Task Post()
{
    SingleComplaint data = new SingleComplaint 
    {
        is_public = ShowPost,
        is_complaint = Complaint,
        show_name = ShowName,
        author = Preferences.Get("UserName", null),
        apt0 = Preferences.Get("Apt", null),
        apartment = Preferences.Get("Apartment", null),
        title = TitleEntry.Text,
        details = DetailsEntry.Text
    };

    try
    {                
        var myApi = RestService.For<IApiService>(Constants.webserver);
        var serialized = JsonConvert.SerializeObject(data);
        ManyComplaints complaint = await myApi.SubmitComplaint(data);
        await DisplayAlert("Thanks", "Your message has been succesfully delivered", "Ok");
    }
    catch (Exception ex)
    {
        await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "Ok");
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试使用该行并将string complaint = await myApi.SubmitComplaint(serialized);其更改为字符串而不是类ManyComplaints,还尝试将模型更改为单一投诉,但我无法让它工作,我缺少什么或者如何让它工作?

小智 11

这个答案可能来得很晚。但最近我正在研究一个类似的用例。下面的代码对我有用。

API声明(使用Refit)

[Headers("Content-Type: application/x-www-form-urlencoded")]
[Get("")]
public HttpResponseMessage GetData([Body(BodySerializationMethod.UrlEncoded)] FormUrlEncodedContent content);
Run Code Online (Sandbox Code Playgroud)

调用API

List<KeyValuePair<string, string>> contentKey = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("apt", "APT"),
    new KeyValuePair<string, string>("apartment", "A103"),
    new KeyValuePair<string, string>("author", "Someone")
};

FormUrlEncodedContent content = new FormUrlEncodedContent(contentKey);

HttpResponseMessage response = someClass.GetData(content);
Run Code Online (Sandbox Code Playgroud)