如何在 C# 中使用 RestSharp POST 方法发送 json 数据
How to send json Data using RestSharp POST Method in c#
我要使用 RestSharp 测试 REST API 并需要 post 方法来 post 数据并基于回调给出状态(错误、无效、成功)我得到 INVALID 即"Object reference not set to an instance of an object"getting json data from header see image
这是我的测试方法
[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);
}
}
我也试过 request.addJsonBody
但我想知道使用 POST 方法
发送 json 数据的结果相同
在您的网络中 api 您需要处理绑定 json 数据。 WebApi 通常自行完成。
而且您不必添加 header 或任何带有 "application/json" 的参数。
如果您可以简单地使用 AddJsonBody(object obj)
方法,RestSharp 将自动设置 header.
或者另一种方法是:
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);
}
并且不要自己序列化数据。 RestSharp 将负责序列化。
我要使用 RestSharp 测试 REST API 并需要 post 方法来 post 数据并基于回调给出状态(错误、无效、成功)我得到 INVALID 即"Object reference not set to an instance of an object"getting json data from header see image
这是我的测试方法
[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);
}
}
我也试过 request.addJsonBody
但我想知道使用 POST 方法
在您的网络中 api 您需要处理绑定 json 数据。 WebApi 通常自行完成。
而且您不必添加 header 或任何带有 "application/json" 的参数。
如果您可以简单地使用 AddJsonBody(object obj)
方法,RestSharp 将自动设置 header.
或者另一种方法是:
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);
}
并且不要自己序列化数据。 RestSharp 将负责序列化。