user3070072
user3070072

Reputation: 620

How to test post method in fiddler?

I am trying to call the following POST method [http://localhost:45361/api/test], in Fiddler, with [Request Header] tab, having [User-Agent: Fiddler, Content-Type: application/json; charset=utf-8] and in the [Request Body],I am passing the following request {"tag":"5667"} . However, its outputting an error -- > Object reference not set to an instance of an 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);
        }

Edit: Query Class:

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; }
  }

I am little unclear, if this is the correct approach to testing post method or if I need add further code in my above controller. Please advice. Many thanks.

Upvotes: 1

Views: 4140

Answers (1)

Umair Ishaq
Umair Ishaq

Reputation: 752

One of the most important things that you need in fiddler is the Content-Type header specification in the post request. Web API has this concept of content negotiation based on request headers and the registration of the content negotiators in the pipeline. Please, see here for more details.

In your case:

Content-Type: application/json; charset=utf-8

Here is the whole request from the fiddler composer:

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

and here is the request body:

{"name":"hello"}

With this post request, you should be able to proceed.

Upvotes: 1

Related Questions