Support Questions
Find answers, ask questions, and share your expertise

Executescript to send HTTPPost

Executescript to send HTTPPost

Hello forum,

I have the below Groovy script which fetch the content of flowfile and send HTTPPost request per line, code is working fine

However, Im not able to put this statement "con = (HttpURLConnection) obj.openConnection()" outside for eachLine loop ?

I don't want to create object for everyline, I want to create the con object once at the begining of handling the flowfile then use it for everyline.. is this possible ?

is it possible to put the code between the //////////// in one method and call it from the eachLine

?

import java.nio.charset.StandardCharsets
import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.net.HttpURLConnection
import java.net.URL
import java.io.BufferedReader
import java.io.DataOutputStream
import java.io.InputStreamReader
def rechargeLocation=""
def url = "http://172.16.24.15:10000"
def flowFile = session.get()
if(!flowFile) return
try {
    URL obj = new URL(url)
    HttpURLConnection con
    BufferedReader br
    DataOutputStream wr
    flowFile = session.write(flowFile, {inputStream, outputStream ->
            inputStream.eachLine { line ->
                    def a = line.split("\\|", -1)
                        if (a[20]!="N/A"){ 
                        rechargeNumber = a[23]
                        rechargeLocation = a[116]
                        //////////////////       
                        con = (HttpURLConnection) obj.openConnection()
                        con.setRequestMethod("POST")
                        con.setRequestProperty("User-Agent", "Mozilla/5.0")
                        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5")
                        con.setDoOutput(true)
                        String urlParameters = "rechargeNumber=" + rechargeNumber + "&rechargeLocation=" + rechargeLocation
                        // Send post request
                        wr = new DataOutputStream(con.getOutputStream())
                        wr.writeBytes(urlParameters)
                        wr.flush()
                        wr.close()
                        int responseCode = con.getResponseCode()
                        br = new BufferedReader(new InputStreamReader(con.getInputStream()))
                        String inputLine
                        StringBuilder response = new StringBuilder()
                        while ((inputLine = br.readLine()) != null) {
                            response.append(inputLine)
                        }
                        br.close()
                        log.warn("Sending " + con.getRequestMethod() + " request to URL: " + url +  ": Response Code: " + responseCode + " :Response: " + response.toString)
                        con.disconnect()
                        //Response = response.toString
                        ///////////////////////////////////////     
                        outputStream.write("${rechargeNumber},${rechargeLocation}\n".toString().getBytes(StandardCharsets.UTF_8))
                    }
            }
        } as StreamCallback)
    session.transfer(flowFile, REL_SUCCESS)
}
catch (IllegalArgumentException  | Exception ex)
{ log.error("ExecuteScript: General Exception. " + ex)
    session.transfer(flowFile, REL_FAILURE)}
Don't have an account?