I tried searching the net for this issue but finally ended up getting no proper answer.
I am trying to post a json data to a URL, I have no idea where my code went wrong, I am getting Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 400
It would be of great help if anyone could help me outwith this issue.
MyCode :
public class JsonTest {
public static void main(String[] args) throws IOException {
try {
URL url = new URL("http://10.48.151.32:30304/transactionManagement/v2/transaction");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
String input = "{\"qty\":100,\"name\":\"iPad 4\"}";
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();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Error Exception :
Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 400
at JsonTest.main(JsonTest.java:36)
My JSON DATA:
{
"request": {
"header": {
"signature": "BHNUMS",
"details": {
"productCategoryCode": "01",
"specVersion": "04"
}
},
"transaction": {
"primaryAccountNumber": "8961060000402122658",
"processingCode": "725400",
"transactionAmount": "000000000200",
"transmissionDateTime": "150505141718",
"systemTraceAuditNumber": "035689",
"localTransactionTime": "141718",
"localTransactionDate": "150505",
"merchantCategoryCode": "0443",
"pointOfServiceEntryMode": "021",
"pointOfServiceConditionCode":"00",
"transactionFeeAmount":"000000000200",
"acquiringInstitutionIdentifier": "10998156762",
"track2Data":";8961060000402122658=4912?",
"retrievalReferenceNumber": "44436440441",
"merchantTerminalId": "87654 987 ",
"merchantIdentifier": "10998156762",
"merchantLocation": "688 PACIFIC HIGHWAYYY CHHHHATSWOOD NSWAU",
"transactionCurrencyCode": "840",
"additionalTxnFields":
{ "productId": "07675018955" ,
"externalAccountNumber":"353142040651369",
}
}
}
}
I am also facing this error .You need to add extra line in your code
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:27.0) Gecko/20100101 Firefox/27.0.2 Waterfox/27.0");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/json; utf-8");
conn.setRequestProperty("Accept", "application/json");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.