[英]HttpClient post returns bad request in c#, works in postman
[英]HTTPClient returns 400 Bad Request but Postman returns 201 in C#
我有以下代碼用於發布請求
[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
}
]
}
}
非常感謝任何幫助。 我用谷歌搜索並閱讀了其他類似的問題,甚至嘗試了其他 stackoverflow 帖子的解決方案,但似乎都沒有用。
這是我在 400 Bad request 旁邊遇到的錯誤
Object 引用未設置為 object 的實例。
單步執行這行代碼后
var result = await client.PostAsync(urlPost, content);
感謝所有的想法,結果我的 json 對於 postman 和我的代碼略有不同,所以 400 錯誤請求實際上是我傳入的 json 的正確結果。(我錯過了必填字段)
抱歉,添麻煩了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.