Search This Blog

Friday, November 10, 2017

Java UniRest to call POST Http/Https Protocols

add - unirest, httpasynclient, httpcore-nio jars

// to get https post

HttpResponse response = Unirest.post ( "https://www.infovijay.com/api/call" ).header ( "content-type", "application/json" ).header ( "cache-control", "no-cache" ).body ( "{\"id\":\"12345\",\"name\":\"vijay\"}\r\n" ).asString ();

System.out.println ( response.getStatus () );
System.out.println ( response.getStatusText () );
System.out.println ( response.getHeaders () );
System.out.println ( response.getBody () );



// to file download

HttpResponse is = Unirest.get ( "https://www.infovijay.com/api/file.pdf" ).asBinary ();
byte [] buffer = new byte[is.getRawBody ().available ()];
is.getRawBody ().read ( buffer );

File targetFile = new File ( "d:/abc.pdf" );
OutputStream outStream = new FileOutputStream ( targetFile );
outStream.write ( buffer );

No comments:

Hit Counter


View My Stats