2

我有我的 angular2 程序,它可以让 http 帖子适合其他操作,但这个程序让我发疯。

这是打字稿DTO:

export class NutritionixSearchRequest
{
  appId: string;
  appKey: string;
  query: string;
  fields: string[];
  sort: {};
  filters: {};
}

当我填写并序列化它时,我得到一个类似于这样的字符串:

        {"appId":"app_id","appKey":"app_key","query":"taco bell",
"fields":"item_name","brand_name","nf_calories","nf_serving_size_qty","nf_serving_size_unit"],
    "sort":{"field":"_score","order":"desc"},"filters":{"item_type":2}}

如果我将此字符串作为帖子请求的主体提供给提琴手,并直接发送到公司的 api,它就可以工作。如果我将请求发送到我的 api,然后调用公司的 api,dto 就不一样了。

我已经尝试了两个版本的 api DTO(如下所示),但均无济于事。第一个返回和对象的排序/过滤器设置为空。如图所示,第二个返回带有“{{”的项目。 当地人展示物品

这个版本根本不起作用,我称公司的api是一样的。这是来自 webapi 2 的序列化对象:

    {"appId":"app_id","appKey":"app_key","query":"taco bell",
"fields":"item_name","brand_name","nf_calories","nf_serving_size_qty","nf_serving_size_unit"],
"sort":[[[]],[[]]],"filters":[[[]]]}

这是dto的:

 public class NutritionixSearchRequestDto
 {
    public string appId { get; set; }
    public string appKey { get; set; }
    public string query { get; set; }
    public ICollection<string> fields { get; set; }
    public ICollection<object> sort { get; set; }
    public ICollection<object> filters { get; set; }
 }

public class NutritionixSearchRequestDto
    {
        public string appId { get; set; }
        public string appKey { get; set; }
        public string query { get; set; }
        public ICollection<string> fields { get; set; }
        public object sort { get; set; }
        public object filters { get; set; }
    }

这是webapi方法:

[Route("nxqlSearch")]
[HttpPost]
public async Task<IHttpActionResult> nxqlSearch([FromBody] NutritionixSearchRequestDto nsrd)
{
   JavaScriptSerializer oJS = new JavaScriptSerializer();
   NutritionixSearchDto nsr = new NutritionixSearchDto();

   using (var client = new HttpClient())
   {
      var searchQuery = oJS.Serialize(nsrd);
      searchQuery.Replace("app_id", appId).Replace("app_key", appKey);
      client.BaseAddress = new Uri(baseUri + "/search");
      client.DefaultRequestHeaders.Accept.Clear();
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

      HttpResponseMessage response = await client.PostAsJsonAsync(new Uri(baseUri + "/search"), searchQuery);
      if (response.IsSuccessStatusCode)
      {
         var results = await response.Content.ReadAsAsync<object>();
         nsr = oJS.Deserialize<NutritionixSearchDto>(results.ToString());
      }
   }

   return Ok(nsr);
}
4

0 回答 0