{"id":6785,"date":"2018-11-12T12:27:05","date_gmt":"2018-11-12T17:27:05","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=6785"},"modified":"2018-11-12T12:27:05","modified_gmt":"2018-11-12T17:27:05","slug":"jython-to-call-rest-service-and-write-output-to-a-file","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2018\/11\/12\/jython-to-call-rest-service-and-write-output-to-a-file\/","title":{"rendered":"Jython to call REST service and write output to a file"},"content":{"rendered":"<pre>\r\nfrom org.apache.http.client.methods import *\r\nfrom org.apache.http.impl.client import *\r\nfrom org.apache.http.client import *\r\nfrom org.apache.http import *\r\nfrom org.json import *\r\nfrom java.io import *\r\nfrom java.util import *\r\nfrom java.lang import *\r\n\r\nclient = HttpClientBuilder.create().build();\r\nurl = \"https:\/\/host\/api\/invoices\";\r\nrq = HttpGet(url);\r\nrq.addHeader(\"Authorization\", \"**guid**\");\r\nrq.addHeader(\"Accept\", \"application\/json\");\r\nrsp = client.execute(rq);\r\nrd = BufferedReader(InputStreamReader(rsp.getEntity().getContent()));\r\nline = \"\";\r\nsb = StringBuffer();\r\nwhile True:\r\n  line = rd.readLine()\r\n  if line == None:\r\n    break\r\n  else:\r\n    sb.append(line);\r\n\r\ns = sb.toString();\r\njo = JSONObject(sb.toString());\r\nja = jo.get(\"invoices\");\r\nitr = ja.iterator();\r\n\r\nwhile (itr.hasNext()):\r\n  s2 = itr.next().toString();\r\n  jo2 = JSONObject(s2);\r\n  invoiceId = jo2.get(\"invoice_id\")\r\n\r\n  url = \"https:\/\/host\/api\/invoices\/\" + str(invoiceId)\r\n  client = HttpClientBuilder.create().build();\r\n  rq = HttpGet(url);\r\n  rq.addHeader(\"Authorization\", \"**guid**\");\r\n  rsp = client.execute(rq);\r\n  ist = rsp.getEntity().getContent();\r\n  filePath = \"\/opt\/tomcat8\/\" + str(invoiceId) + \".pdf\";\r\n  fos = FileOutputStream(File(filePath));\r\n  while True:\r\n    inByte = ist.read()\r\n    if inByte == -1:\r\n      break\r\n    else:\r\n      fos.write(inByte);\r\n\r\n  ist.close();\r\n  fos.close();\r\n-bash-4.1$ ls -lrt *.pdf\r\n-rw-r--r-- 1 tomcat tomcat 62221 Nov  8 12:57 230187.pdf\r\n-rw-r--r-- 1 tomcat tomcat 62269 Nov  8 12:57 230189.pdf\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>from org.apache.http.client.methods import * from org.apache.http.impl.client import * from org.apache.http.client import * from org.apache.http import * from org.json import * from java.io import * from java.util import * from java.lang import * client = HttpClientBuilder.create().build(); url = &#8220;https:\/\/host\/api\/invoices&#8221;; rq =&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2018\/11\/12\/jython-to-call-rest-service-and-write-output-to-a-file\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[24,106],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6785"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=6785"}],"version-history":[{"count":1,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6785\/revisions"}],"predecessor-version":[{"id":6786,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6785\/revisions\/6786"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=6785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=6785"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=6785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}