Skip to Content
0
Former Member
Aug 11, 2016 at 06:37 AM

Error(403) while Posting data using XSOData Service into HCP from Java application

394 Views

Hi Experts,

Scenario : I am trying to post data into Hana Cloud Platform using xsodata service from java application which is hosted in local Eclipse IDE.

We are able to GET data from the XSOData service but while posting using same service we are getting 403 forbidden error.

I am also fetching the CSRF token in GET request and including the same in the POST request.

But I am able to successfully test both scenarios GET and POST through POSTMAN Rest Client.

----------------------------------------------------------------------------------

(Code that we are using for GET Request from Java Standalone Application )

String code = "Basic "+new String(Base64.encodeBase64

(("UserNamexxxxxx:Passwordxxxxxxx").getBytes()));

URL absUrl = new URL

("https://xxxxx.hana.ondemand.com/ProjectName/SomeServiceName/SomeSchema.xsodata/SomeTable?$format=json");

HttpURLConnection con = (HttpURLConnection)absUrl.openConnection();

con.setRequestMethod("GET");

con.setRequestProperty("Authorization", code);

con.setRequestProperty("X-CSRF-Token", "Fetch");

con.connect();

String result = extractXrsfToken (con);

System.out.println("X-CSRF token value : "+result);

BufferedReader br = postCon(result);

----------------------------------------------------------------------------------

-----------------------------------------------------------------------------------

(Code that we are using in POST Request from Java Standalone Application)

private BufferedReader postCon(String result)throws Exception {

JSONObject json = new JSONObject();

json.accumulate("ID","value");

json.accumulate("SomeID","someValue");

json.accumulate("SomeID","someValue");

json.accumulate("SomeID","someValue");

json.accumulate("SomeID","someValue");

System.out.println("Printing Json Input \n "+json.toString());

String jsonString = json.toString();

byte[] jsonBytes = jsonString.getBytes(StandardCharsets.UTF_8);

int jsonbyteslength = jsonBytes.length;

String code = "Basic "+new String(Base64.encodeBase64

(("UserNamexxxxxx:Passwordxxxxxxx").getBytes()));

URL absUrl = new URL("https://xxxxx.hana.ondemand.com/ProjectName/SomeServiceName/SomeSchema.xsodata/SomeTable");

HttpURLConnection con = (HttpURLConnection)absUrl.openConnection();

con.setDoInput(true);

con.setDoOutput(true);

con.setRequestMethod("POST");

con.setRequestProperty("Authorization", code);

con.setRequestProperty("X-CSRF-Token", result);

con.setRequestProperty("Content-Type", "application/json");

con.setRequestProperty("Accept", "application/json");

con.setRequestProperty( "charset", "utf-8");

con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");

DataOutputStream wr = new DataOutputStream(con.getOutputStream());

wr.write(jsonBytes);

con.connect();

System.out.println(" Result Printing in process ");

BufferedReader res = new BufferedReader(new InputStreamReader

(con.getInputStream()));

StringBuffer br = new StringBuffer();

String line;

while((line = res.readLine())!= null){

br.append(line);

}

System.out.println("Printing Buffered OP");

System.out.println(br);

System.out.println("Printing Buffered OP Finished");

System.out.println("End of Post ");

return res;

}

(Code the we are using for gettingCRSF Token)

private String extractXrsfToken(HttpURLConnection conn) {

List<String> value = null;

Map<String, List<String>> headers = conn.getHeaderFields();

Iterator<String> keys = headers.keySet().iterator();

while (keys.hasNext()) {

String key = keys.next();

if ("X-CSRF-Token".equalsIgnoreCase(key)) {

value = headers.get(key);

}

}

if (value == null || value.size() == 0) {

return null;

} else {

return value.get(0);

}

}

----------------------------------------------------------------------------------

Please help me find the solution the occurring issue

Thanks

Abhijith