我有以下用于发帖请求的代码
[TestMethod]
public async Task<Tuple<int, string>> PostRequestAdditionalAttributeValid0000()
{
string locationPath = solutionDirectory.ToString();
var jsonText = File.ReadAllText(locationPath + @"\TestJsons\testdata.json");
using (var client = new HttpClient())
{
var content = new StringContent(json, Encoding.UTF8, "application/json");
//Add client header
client.DefaultRequestHeaders.Add("client_uuid", "2fd77dd8-ed76-4bba-b0e1-5cda454c8d6e");
var result = await client.PostAsync(urlPost, content);
int StatusNumber = (int)result.StatusCode;
string resultContent = await result.Content.ReadAsStringAsync();
return new Tuple<int, string>(StatusNumber, resultContent);
}
}
,在Postman中返回201,但HTTPClient返回400响应(错误请求)。我不知道为什么。
我的json如下
{
"audit_date": "2020-05-13T11:27:10.3187798Z",
"client_uuid": "2fd77dd8-ed76-4bba-b0e1-5cda454c8d6e",
"audit_entry": {
"where_uri": "test.com/apps/171f0841-825b-4964-8f8c-0869650f14a6",
"why_uri": "test.com/reference/reasons_for_change/61acc173-7168-4ae5-9f04-afa228941f8b",
"who_uri": "test.com/users/4977dae1-a307-425f-980c-53413fef1b0f",
"when_audited": "2018-11-13T20:20:39+00:00",
"what_uri": "test.com/tests/1bc67a71-8549-4ab8-9dd9-e44238198860",
"what_changed": [
{
"attribute_name": "birth_year",
"attribute_value": "1969",
"attribute_change": null
},
{
"attribute_name": "subject_reference",
"attribute_value": "TEST-WOO3444",
"attribute_change": null
}
]
}
}
任何帮助深表感谢。我已经在Google上搜索并阅读了其他类似的问题,甚至尝试了其他stackoverflow帖子中的解决方案,但似乎都没有效果。
这是我在遇到400错误请求时遇到的错误
对象引用未设置为对象的实例。
浏览了这一行代码之后
var result = await client.PostAsync(urlPost, content);
答案 0 :(得分:0)
感谢所有想法,结果发现我的json对于邮递员和我的代码略有不同,因此400错误的请求实际上是我传递的json的正确结果。(我错过了必填字段)
很抱歉造成麻烦。