Kolege
treba mi neka sugestija za resavanje problema kod upload fajla preko REST post na web servis.
Odgovor web servisa je status code 422 {"status":"failed","error":{"data":["Parametar data je obavezan"],..............
Fajl sam za probu ucitala preko Swagger UI i dobila sledeci rezultat (tackice zamenjuju prave podatke kao name i email)
curl -X 'POST' \
'https://............/send/file/xml' \
-H 'accept: application/json' \
-H 'authorization-general: Bearer 36...........' \
-H 'Authorization: Bearer ey...........' \
-H 'Content-Type: multipart/form-data' \
-F '[email protected];type=text/xml' \
-F 'type=0' \
-F 'contacts[0][name]=aaaaaa' \
-F 'contacts[0][email][email protected]' \
na osnovu toga sam napisala clijent program
**************************************************
........
CloseableHttpClient httpclient = null;
String boundary;
try {
boundary = "--" + System.currentTimeMillis();
InputStream responseStream;
BufferedReader br;
String url = "
https://.........../send/file/xml";
File file = new File("e:/............../012023IV260.xml");
HttpPost post = new HttpPost(url);
post.addHeader("accept", "application/json");
post.addHeader("authorization-general", "Bearer 36.............");
post.addHeader("Authorization", "Bearer ey..............");
post.addHeader("Content-Type","multipart/form-data; boundary="+ boundary);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("data", file,ContentType.TEXT_XML, file.getName());
builder.setBoundary(boundary);
builder.addTextBody("type", "0");
builder.setBoundary(boundary);
builder.addTextBody("contacts[0][name]","aaaaa");
builder.setBoundary(boundary);
builder.addTextBody("contacts[0][email]","
[email protected]");
builder.setBoundary(boundary+"--");
httpclient = HttpClientBuilder.create().build();
HttpEntity entity = builder.build();
post.setEntity(entity);
HttpResponse response = httpclient.execute(post);
if (response != null) {
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Status code: " + statusCode);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
responseStream = responseEntity.getContent() ;
if (responseStream != null){
br = new BufferedReader (new InputStreamReader (responseStream)) ;
String responseLine = br.readLine() ;
String tempResponseString = "" ;
while (responseLine != null){
tempResponseString = tempResponseString + responseLine + System.getProperty("line.separator") ;
responseLine = br.readLine() ;
}
br.close() ;
if (tempResponseString.length() > 0){
System.out.println(tempResponseString);
}
}
responseStream.close();
}
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*************************************
Hvala unapred za bilo kakvu sugestiju