Py Script snap
I am building a pipeline which need to make recurring API call and consolidate the result. But when I call the API inside py script snap then it does not return result as the API default result. I tried to use two methods of get as get.getResponseBodyAsString() and get.getResponseBody()
get.getResponseBodyAsString() → return response as string
get.getResponseBody() → returns “Base64 encoded data”
So I am stuck here, where I need the result in json format which is default API response type, but it is creating issue. Below is the code for this.
from com.snaplogic.scripting.language import ScriptHook
from com.snaplogic.scripting.language.ScriptHook import *
from org.apache.commons.httpclient import HttpClient
from org.apache.commons.httpclient.methods import GetMethod;
class UploadScript(ScriptHook):
def init(self, input, output, error, log):
self.input = input
self.output = output
self.error = error
self.log = log
def execute(self):
while self.input.hasNext():
data = self.input.next()
url = "https://gorest.co.in/public-api/products"
get = GetMethod(url)
client = HttpClient()
r = client.executeMethod(get)
#s = get.getResponseBodyAsString()
s = get.getResponseBody()
t = get.getStatusText()
h = get.getResponseHeaders()
self.output.write(s)
self.log.info("Finished executing the upload script")
hook = UploadScript(input, output, error, log)