如何将JSON数据从fiddler发布到.net中的api Controller [英] How to POST JSON data from fiddler to api Controller in .net

查看:53
本文介绍了如何将JSON数据从fiddler发布到.net中的api Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型类产品:

公共类产品
{
public int Id {get;组; }
public string Name {get;组; }
public string Category {get;组; }
公共小数价格{get;组; }
}



和API控制器是ProductsController,Post方法是:

 [HttpPost] 
public HttpResponseMessage PostProduct(Product item)
{
if(item == null)
返回新的HttpResponseMessage(HttpStatusCode.BadRequest);

item = repository.Add(item);
var response = Request.CreateResponse< Product>(HttpStatusCode.Created,item);

string uri = Url.Link(DefaultApi,new {id = item.Id});
response.Headers.Location = new Uri(uri);
返回回复;
}



当我从fiddler Product项发布JSON数据时,我得到null值,但get方法正常。

请有人帮我解决这个问题。

解决方案





您可以解决以下问题:



请求标题:



用户代理: Fiddler

主持人:localhost:4217

内容类型:application / json; charset = utf-8



请求机构



= {Number1 :7,Number2:7}



请点击以下链接获取图片帮助:



http://atgsoftware.com/blog/testing-web-api- post-with-fiddler / [ ^ ]

I have a model class Product as:

public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }


and the API controller is ProductsController and the Post method is:

 [HttpPost]
public HttpResponseMessage PostProduct(Product item)
{
    if (item == null)
        return new HttpResponseMessage(HttpStatusCode.BadRequest);

    item = repository.Add(item);
    var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item);

    string uri = Url.Link("DefaultApi", new { id = item.Id });
    response.Headers.Location = new Uri(uri);
    return response;
}


When I post JSON data from fiddler Product item, I get null value, but get method works fine.
Please anyone help me to solve this problem.

解决方案

Hi,

You can solve your issue to follow these:

Request Header:

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

Request Body

={"Number1":"7","Number2":"7"}

Please follow following link for help by images :

http://atgsoftware.com/blog/testing-web-api-post-with-fiddler/[^]


这篇关于如何将JSON数据从fiddler发布到.net中的api Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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