1
我試圖將下面的curl轉換爲java,我得到錯誤400OpenFigi API curl到java POST
400請求正文必須是一個數組。請求主體不是數組。
捲曲-v -X POST 'https://api.openfigi.com/v1/mapping'
--header '內容 - 類型:文本/ JSON'
--data「[{ 「idType」: 「ID_WERTPAPIER」, 「idValue」: 「851399」 「exchCode」: 「US」}]」
公共無效R2()拋出MalformedURLException的,IOException異常{
//String str = "https://www.openfigi.com/search#!?marketSector=Comdty";
String str = "https://api.openfigi.com/v1/mapping";
URL url = new URL(str);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/json");
String input = "'[{\"idType\":\"ID_WERTPAPIER\",\"idValue\":\"851399\",\"exchCode\":\"US\"}]'";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
/*
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}*/
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
}
感謝
如果你在'input'的開頭和結尾去掉'''字符,它應該可以工作。 –