I am learning rest API and I find it difficult to do the PUT request. Below is the code which I tried. But the record doesn't get updated successfully
The URL used - http://www.thomas-bayer.com/sqlrest/PRODUCT/1
Value trying to update - <PRICE>24.8</PRICE> to <PRICE>24.9</PRICE>
Language : Java
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPut putrequest = new HttpPut(
"http://www.thomas-bayer.com/sqlrest/PRODUCT/1");
List<NameValuePair> nvps = new ArrayList<NameValuePair>() {{
add(new BasicNameValuePair("content-type", "application/xml"));
add(new BasicNameValuePair("Server", "Apache-Coyote/1.1"));
}};
for (NameValuePair h : nvps) {
putrequest.addHeader(h.getName(), h.getValue());
}
String xml="<?xml version=\"1.0\"?><PRODUCT xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n" +
" <ID>1</ID>\n" +
" <NAME>Chair Shoe</NAME>\n" +
" <PRICE>24.9</PRICE>\n" +
"</PRODUCT>";
putrequest.setEntity(new StringEntity(xml));
HttpResponse response = httpClient.execute(putrequest);
if (response.getStatusLine().getStatusCode() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
httpClient.getConnectionManager().shutdown();
}
I get the 500 response but I am able to add the record if the record is not there using HTTPPUT. How to fix this?
If it's getting added and still you are getting 500, then there are below possibilities: