HTTPClient returns 400 Bad Request 但 C# 中的 Postman returns 201
HTTPClient returns 400 Bad Request but Postman returns 201 in C#
我有以下代码用于 post 请求
[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 中它 returns 一个 201 但 HTTPClient returns 一个 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
}
]
}
}
非常感谢任何帮助。我用谷歌搜索并阅读了其他类似的问题,甚至尝试了其他 Whosebug post 的解决方案,但 none 似乎有效。
这是我在 400 错误请求旁边遇到的错误
对象引用未设置为对象的实例。
单步执行这行代码后
var result = await client.PostAsync(urlPost, content);
感谢所有想法,结果我的 json 对于邮递员和我的代码略有不同,所以 400 错误请求实际上是我传递的 json 的正确结果。 (我错过了必填字段)
抱歉给您带来麻烦。
我有以下代码用于 post 请求
[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 中它 returns 一个 201 但 HTTPClient returns 一个 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
}
]
}
}
非常感谢任何帮助。我用谷歌搜索并阅读了其他类似的问题,甚至尝试了其他 Whosebug post 的解决方案,但 none 似乎有效。
这是我在 400 错误请求旁边遇到的错误
对象引用未设置为对象的实例。
单步执行这行代码后
var result = await client.PostAsync(urlPost, content);
感谢所有想法,结果我的 json 对于邮递员和我的代码略有不同,所以 400 错误请求实际上是我传递的 json 的正确结果。 (我错过了必填字段)
抱歉给您带来麻烦。