创建一个从控制台的WebAPI的帖子,包括在JSON数据$类型 [英] Create WebAPI post from Console to include $type in json data
本文介绍了创建一个从控制台的WebAPI的帖子,包括在JSON数据$类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建对象,并张贴到一个的WebAPI。基本上,我只是不能织补事情序列化,以便包括在JSON的$类型信息。以下是code我试图写。后来是我所期望的JSON。
VAR CDS =新的List< CreditDefaultSwaps>()
{
新CreditDefaultSwaps(){ModelNumber =SP8A1ETA,经纪人$ P $垫= 0},
新CreditDefaultSwaps(){ModelNumber =SP3A0TU1,经纪人$ P $垫= 0},
新CreditDefaultSwaps(){ModelNumber =SP4A102V,经纪人$ P $垫= 0}
}; VAR的客户=新的HttpClient {BaseAddress =新的URI(HTTP://本地主机/ BloombergWebAPI / API /)}; client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(新MediaTypeWithQualityHeaderValue(应用/ JSON)); //设置请求对象
VAR oContract =新WebApiDataServiceRequest
{
请求类型= ReferenceDataRequestServiceTypes.ReferenceDataRequest,
SwapType = BloombergWebAPIMarshal.SwapType.CDS,
SecurityList = CDS
}; 尝试过这样的事情和VAR含量格式化为我期望
然而,我无法发布使用postasjsonasync数据 // VAR内容= JsonConvert.SerializeObject(oContract,Formatting.Indented,
//新JsonSerializerSettings {TypeNameHandling = TypeNameHandling.Objects}); 到Console.ReadLine(); VAR响应= client.PostAsJsonAsync(bloombergapi / processbloombergrequest,oContract)。结果;
下面是我想张贴JSON。那我在上面code缺失,我敢肯定它的一些愚蠢的。
{
$类型:BloombergWebAPIMarshal.WebApiDataServiceRequest,BloombergWebAPIMarshal
的RequestType:3,
SwapType:1,
SecurityList:[
{
$类型:BloombergWebAPIMarshal.CreditDefaultSwaps,BloombergWebAPIMarshal
ModelNumber:SP8A1ETA,
经纪人$ P $垫:0
},
{
$类型:BloombergWebAPIMarshal.CreditDefaultSwaps,BloombergWebAPIMarshal
ModelNumber:SP3A0TU1,
经纪人$ P $垫:0
},
{
$类型:BloombergWebAPIMarshal.CreditDefaultSwaps,BloombergWebAPIMarshal
ModelNumber:SP4A102V,
经纪人$ P $垫:0
}
]
}
解决方案
创建用于该调用来产生适当的请求,另一个重载:
VAR响应= client.PostAsJsonAsync(processbloombergrequest,oContract,TypeNameHandling.Objects)。结果
这是新的过载
公共静态任务< Htt的presponseMessage> PostAsJsonAsync< T>(这HttpClient的客户端,串requestUri,T值,TypeNameHandling typeNameHandling)
{ 返回client.PostAsJsonAsync< T>(requestUri,价值,CancellationToken.None,typeNameHandling);
}公共静态任务< Htt的presponseMessage> PostAsJsonAsync< T>(这HttpClient的客户端,串requestUri,T值,的CancellationToken的CancellationToken,TypeNameHandling typeNameHandling)
{
VAR格式=新JsonMediaTypeFormatter
{
SerializerSettings =新JsonSerializerSettings()
{
TypeNameHandling = typeNameHandling
}
}; 返回client.PostAsync< T>(requestUri,价值,格式,的CancellationToken);
}
I'm creating objects and posting them to a webapi. Basically I just can't get the darn things to serialize so as to include the $type info in the json. The following is the code I'm attempting to write. Afterwards is the json I would expect.
var cds = new List<CreditDefaultSwaps>()
{
new CreditDefaultSwaps() { ModelNumber = "SP8A1ETA", BrokerSpread = 0},
new CreditDefaultSwaps() { ModelNumber = "SP3A0TU1", BrokerSpread = 0},
new CreditDefaultSwaps() { ModelNumber = "SP4A102V", BrokerSpread = 0}
};
var client = new HttpClient {BaseAddress = new Uri("http://localhost/BloombergWebAPI/api/")};
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// set up request object
var oContract = new WebApiDataServiceRequest
{
RequestType = ReferenceDataRequestServiceTypes.ReferenceDataRequest,
SwapType = BloombergWebAPIMarshal.SwapType.CDS,
SecurityList = cds
};
Tried something like this and the var content was formatted as I would expect
however I couldn't post the data using postasjsonasync
//var content = JsonConvert.SerializeObject(oContract, Formatting.Indented,
// new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects });
Console.ReadLine();
var response = client.PostAsJsonAsync("bloombergapi/processbloombergrequest", oContract).Result;
The following is the json I'm trying to post. What am I missing in the above code, I'm sure it's something silly.
{
"$type": "BloombergWebAPIMarshal.WebApiDataServiceRequest, BloombergWebAPIMarshal",
"RequestType": 3,
"SwapType": 1,
"SecurityList": [
{
"$type": "BloombergWebAPIMarshal.CreditDefaultSwaps, BloombergWebAPIMarshal",
"ModelNumber": "SP8A1ETA",
"BrokerSpread": 0
},
{
"$type": "BloombergWebAPIMarshal.CreditDefaultSwaps, BloombergWebAPIMarshal",
"ModelNumber": "SP3A0TU1",
"BrokerSpread": 0
},
{
"$type": "BloombergWebAPIMarshal.CreditDefaultSwaps, BloombergWebAPIMarshal",
"ModelNumber": "SP4A102V",
"BrokerSpread": 0
}
]
}
解决方案
Created another overload Used this call to produce proper request:
var response = client.PostAsJsonAsync("processbloombergrequest", oContract, TypeNameHandling.Objects).Result
This is the new overload
public static Task<HttpResponseMessage> PostAsJsonAsync<T>(this HttpClient client, string requestUri, T value, TypeNameHandling typeNameHandling)
{
return client.PostAsJsonAsync<T>(requestUri, value, CancellationToken.None, typeNameHandling);
}
public static Task<HttpResponseMessage> PostAsJsonAsync<T>(this HttpClient client, string requestUri, T value, CancellationToken cancellationToken, TypeNameHandling typeNameHandling)
{
var formatter = new JsonMediaTypeFormatter
{
SerializerSettings = new JsonSerializerSettings()
{
TypeNameHandling = typeNameHandling
}
};
return client.PostAsync<T>(requestUri, value, formatter, cancellationToken);
}
这篇关于创建一个从控制台的WebAPI的帖子,包括在JSON数据$类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文