#java #groovy #apache-nifi
#java #groovy #apache-nifi
Вопрос:
Ниже приведен код, который я написал в Groovy
def flowFile = session.get()
def postResult=''
def url = new URL("https://instrument-api.systems.uk.hsbc/gbm/ts/financial-instrument/v1/exchange-calendars?calendarCode=Hns")
HttpURLConnection connection = (HttpURLConnection) url.openConnection()
connection.setRequestMethod("GET")
//connection.setConnectTimeout(10000)
connection.connect()
rMsg = connection.getResponseMessage()
rCode= connection.getResponseCode()
flowFile = session.putAttribute(flowFile, 'api_response_code', rCode.toString())
flowFile = session.putAttribute(flowFile, 'api_response_message', rMsg)
if (rCode == 200) {
flowFile = session.write(flowFile, {outputStream ->
outputStream.write(connection.content.getBytes())
} as OutputStreamCallback)
connection.disconnect();
session.transfer(flowFile,REL_SUCCESS)
} else {
flowFile = session.putAttribute(flowFile, 'api_response_error', connection.getErrorStream().toString())
flowFile = session.putAttribute(flowFile, 'api_response_message', connection.getInputStream())
session.transfer(flowFile,REL_FAILURE)
}
Когда я запускаю это через postman, он выдает мне код ответа 500 со следующим текстом в теле:
"timestamp": "2021-01-15T10:47:54.938 0000",
"status": 500,
"error": "Internal Server Error",
"message": "JwtTokenMissingException: No JWT token found in request headers",
"path": "/xxx/xx/xxxxxxxxx/v1/exchange-caxxxxx"
}
Но я не могу получить это тело из моего скрипта groovy. Если я попытаюсь использовать connection.getInputStream()
or connection.content.getBytes()
, он вернет мне исключение нулевого указателя.
Комментарии:
1. 1) отсутствует
if(!flowFile)return
после session.get. 2) при возврате http-кода с ошибкой вам необходимо прочитать содержимое из getErrorStream ()2. почему бы вам просто не использовать процессор InvokeHTTP?