我有一臺服務器,我想以 Java 中的 json 形式向其發送發布請求。但發送后,它給出了答案:“KeyError: 400 Bad Request: The browser (or proxy) send an request that this server could not understand.”。不管我怎么改資料,還是報這個錯,發送的請求也沒有出現在服務器上。我怎樣才能解決這個問題?
URL url = new URL("http://buldakovn.pythonanywhere.com/addStudent");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
//request body
String str1 = "{FName: " name ", LName: " surname ", VkId: " vk ", TelegrammId: " telegram ", Group: " group "}";
OutputStream out = connection.getOutputStream();
byte [] input = str1.getBytes(StandardCharsets.UTF_8);
out.write(input, 0, input.length);
out.close();
uj5u.com熱心網友回復:
您需要做的第一件事是查看您要發布到的端點并確定它接受什么。我的意思是,你確定它需要json嗎?你確定你的有效載荷是正確的嗎?
真的沒有辦法繞過它,要使用一個 API,你需要知道它期望什么。
其次,您的 Stringstr1不包含有效的 JSON,因此即使您的所有欄位和型別都正確,這也行不通。
有效的 Json 將讀作
{"FName": "someName", "LName": "someName"}
你有什么產生這個
{FName: someName, LName: someName}
注意到字串中缺少引號了嗎?甚至 StackOverflow 決議器也不知道該怎么做:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/477058.html
上一篇:從兩個另一個創建一個陣列
