簡體   English   中英

如何在提琴手中測試發布方法?

[英]How to test post method in fiddler?

我試圖用[Request Header]選項卡在Fiddler中調用以下POST方法[http://localhost:45361/api/test] ,具有[User-Agent: Fiddler, Content-Type: application/json; charset=utf-8] [User-Agent: Fiddler, Content-Type: application/json; charset=utf-8]然后在[請求正文]中傳遞以下請求{"tag":"5667"} 但是,其輸出錯誤-> 對象引用未設置為object的實例

    [HttpPost]
    public HttpResponseMessage post([FromBody] Query query)
    {
            IQueryable<data_qy> Data = null;

            if (!string.IsNullOrEmpty(query.tag)) //--> line causing the ERROR
            {
                var ids = query.tag.Split(',');
                var dataMatchingTags = db.data_qy.Where(c => ids.Contains(c.TAG));

                if (Data == null)
                    Data = dataMatchingTags;
                else
                    Data = Data.Union(dataMatchingTags);
            }

            if (!string.IsNullOrEmpty(query.name))
            {
                var ids = query.name.Split(',');
                var dataMatchingTags = db.data_qy.Where(c => ids.Any(id => c.Name.Contains(id)));

                if (Data == null)
                    Data = dataMatchingTags;
                else
                    Data = Data.Union(dataMatchingTags);
            }

            if (Data == null) 
                Data = db.data_qy;

            if (query.endDate != null)
            {
                Data = Data.Where(c => c.UploadDate <= query.endDate);
            }

            if (query.startDate != null)
            {
                Data = Data.Where(c => c.UploadDate >= query.startDate);
            }

            var data = Data.ToList();

            if (!data.Any())
            {
                var message = string.Format("No data found");
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
            }

            return Request.CreateResponse(HttpStatusCode.OK, data);
        }

編輯:查詢類:

public class Query
  {

    public string name { get; set; }
    public string tag{ get; set; }
    public Nullable<DateTime> startDate { get; set; }
    public Nullable<DateTime> endDate{ get; set; }
  }

我不清楚,這是否是測試post方法的正確方法,或者是否需要在上述控制器中添加更多代碼。 請指教。 非常感謝。

提琴手中需要的最重要的事情之一就是發布請求中的Content-Type標頭規范。 Web API具有基於請求標頭和內容協商程序在管道中注冊的內容協商的概念。 請參閱此處以獲取更多詳細信息。

在您的情況下:

內容類型:application / json; 字符集= utf-8的

這是提琴手作曲家的全部要求:

User-Agent: Fiddler
Host: localhost:26572
Content-Length: 16
Content-Type: application/json; charset=utf-8

這是請求正文:

{"name":"hello"}

有了此發布請求,您應該可以繼續。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM