简体   繁体   中英

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

this is my 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

in your web api you need to handle the binding json data. WebApi does it by itself normally. And also you dont have to add header or any parameter with "application/json". If you could just simply use AddJsonBody(object obj) method RestSharp will automatically set the header.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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