简体   繁体   English

如何在C#中使用RestSharp POST方法发送JSON数据

[英]How to send json Data using RestSharp POST Method in c#

im going to test REST API using RestSharp and required post method to post data and base on callback give status (error,invalid,success) im geting INVALID that is "Object reference not set to an instance of an object" getting json data from header see image 我将使用RestSharp和所需的post方法测试REST API,以发布数据并基于回调给出状态(错误,无效,成功)。im获取INVALID,即“对象引用未设置为对象的实例” ,从标头获取json数据看图片

this is my TestMethod 这是我的TestMethod

     [TestMethod()]
            public void AddNewBFormat()
            {
            Random r = new Random((int)DateTime.Now.Ticks);
            var x = r.Next(100000, 999999);
            string s = x.ToString("000000");

            string UniqueFileName = "S" + s + DateTime.Now.ToString("yyyyMMdd") + ".xlsx";
            request.Resource = "api/BFormat/AddNewBFormat";
            request.Method = Method.POST;
            var body= "{'UploadFileVM':{'BordereauxId':null,'BFormatId':null,'FileName':'"+UniqueFileName+ "','Filesize':0,'Path':'C:\\Applications\\new\\\\TempUploadedFiles','size':0,'ActiveSheetIndex':0,'HeaderIndex':0,'MultiHeaders':null,'SheetNames':null,'IsPasswordProtected':false},'BFormat':{'UniqueFileName':'"+ UniqueFileName+"'}}";
            request.RequestFormat = DataFormat.Json;
            request.AddHeader("Content-type", "application/json");
            request.AddParameter("Application/Json", body, ParameterType.RequestBody);
            var queryResult = client.Execute<ResponseData<Guid>> (request).Data;
            try
            {
                Assert.IsTrue(queryResult.ReturnData != null);

            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            }

i have tried request.addJsonBody also but same result i want to know to send json data using POST Method 我也尝试过request.addJsonBody但结果相同,我想知道使用POST方法发送json数据

in your web api you need to handle the binding json data. 在您的Web API中,您需要处理绑定的json数据。 WebApi does it by itself normally. WebApi通常会自己完成。 And also you dont have to add header or any parameter with "application/json". 而且,您也不必使用“ application / json”添加标题或任何参数。 If you could just simply use AddJsonBody(object obj) method RestSharp will automatically set the header. 如果您可以简单地使用AddJsonBody(object obj)方法,RestSharp将自动设置标头。

Or another way to do that is: 或另一种方法是:

    Random r = new Random((int)DateTime.Now.Ticks);
    var x = r.Next(100000, 999999);
    string s = x.ToString("000000");
    string UniqueFileName = "S" + s + DateTime.Now.ToString("yyyyMMdd") + ".xlsx";
    request.Resource = "api/BFormat/AddNewBFormat";
    request.Method = Method.POST;
    request.RequestFormat = DataFormat.Json;
    var body = new
    {
        UploadFileVM = new
        {
            BordereauxId = "",
            BFormatId = "",
            FileName = UniqueFileName,
            Filesize = 0,
            Path = @"c:\sdakdldas\"
        }
    };
    request.AddBody(body); //enter code herE
    var queryResult = client.Execute<ResponseData<Guid>>(request).Data;
    try
    {
        Assert.IsTrue(queryResult.ReturnData != null);

    }
    catch (Exception ex)
    {
        Assert.Fail(ex.Message);
    }

And don't serialize the data by urself. 并且不要自己序列化数据。 RestSharp will take care of serializing. RestSharp将负责序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM