Maximo

 View Only
Expand all | Collapse all

Decompress Gzip response content from External Rest API through Automation Script

  • 1.  Decompress Gzip response content from External Rest API through Automation Script

    Posted Tue July 05, 2022 09:26 AM
    I am using the automation script to POST requests to Oracle Rest API Endpoint. With the response code success <300, I can parse the JSON response and get the value from that payload. However, the error response in the case of failing with response code >300, the Oracle API response with the gzip-compressed. I can not parse this to the JSON. I tried to decompress it, but it seems to not work.
    I am using router.invoke to post the request. 
    response = String(router.invoke(map, jsonStr))
    responseObject=JSONObject.parse(response) => occur an error 

    This is the response header:
    Content-Type:application/vnd.oracle.adf.error+json
    Content-Encoding:gzip

    Is there any method to decompress this JSON payload? Please help me if you did it before. 
    Thanks
    IBM Maximo Asset Management 7.6.1.0 Build 20180718-1141 DB Build V7604-01





    ------------------------------
    Thi Minh Man Nguyen
    ------------------------------



    #Maximo
    #AssetandFacilitiesManagement
    #MaximoIntegrationandScripting


  • 2.  RE: Decompress Gzip response content from External Rest API through Automation Script

    Posted Wed July 06, 2022 10:02 AM
    Not sure if this will help, but take a look at this page. https://ibm-maximo-dev.github.io/maximo-autoscript-documentation/integration/endpointhandler
    I used processResponse(resp) to do this.

    httpcode = resp.getResponseCode()

    Check for httpcode > 300 and you can raise an exception.

    ------------------------------
    MANU MAGANTI
    EMA Canada| Inc.
    OAKVILLE ON
    ------------------------------



  • 3.  RE: Decompress Gzip response content from External Rest API through Automation Script

    Posted Thu July 07, 2022 09:58 PM
    Thanks for your help, @MANU MAGANTI
    But the ScriptHTTPResp is available from 7.6.1.2. It doesn't exist in our version 7.6.1.0
    I can get the response code by using the errorResponse.get("RESPONSE_STATUS"). I can not read the gzip compressed content response. 
    router = Router.getHandler("ENDPOINT")
    map = HashMap()
    errorResponse = HashMap()
    errortext = HashMap()
    map.put("ERRORONSTATUS",False)
    map.put("RESPONSE_HEADERS",errorResponse)
    map.put("RESPONSE_STATUS_TEXT",False)
    response = String(router.invoke(map, jsonStr))
    if int(errorResponse.get("RESPONSE_STATUS")) < 300:
          responseObject=JSONObject.parse(response)
          service.setWarning("MXOF","MESS",["The Item has been successfully created in external system", responseObject['ItemNumber']])
    else:
          responseObject=JSONObject.parse(response) => occur an error here because of the gzip compressed content
          service.error("MXOF","MESS",["Fail to integration ",responseObject['ErrorMessage']])​​​

    ------------------------------
    Thi Minh Man Nguyen
    ------------------------------



  • 4.  RE: Decompress Gzip response content from External Rest API through Automation Script

    Posted Fri July 08, 2022 01:01 AM
    Edited by System Tue August 22, 2023 04:37 PM
    Did you try something like this?

    byteResponse = handler.invoke(handlerMap, None)
    jsonObject = OslcUtils.bytesToJSONObject(byteResponse)


    Also, take a look at this. Search for something similar - https://stackoverflow.com/questions/35290083/how-to-decode-a-source-code-which-is-compressed-with-gzip-in-python
    ------------------------------
    MANU MAGANTI
    EMA Canada| Inc.
    OAKVILLE ON
    ------------------------------



  • 5.  RE: Decompress Gzip response content from External Rest API through Automation Script

    Posted Fri July 08, 2022 04:04 AM
    I already tried the OslcUtils.bytesToJSONObject. It throws with error: "Error parsing JSON data.
    Unexpected character: [] while scanning JSON String for JSON type. Invalid JSON"
    Yes, my issue is similar to the topic in the link. I tried many ways to decompress it, but still not successful. 
    Thanks for your help. 

     


    ------------------------------
    Thi Minh Man Nguyen
    ------------------------------



  • 6.  RE: Decompress Gzip response content from External Rest API through Automation Script

    Posted Fri July 08, 2022 08:18 AM
    Your error is getting thrown because of this first line here, right? You already know it's not a JSON object so you should be working with just response and trying to decompress that. Does just service.error work if you don't reference responseObject?

    responseObject=JSONObject.parse(response) => occur an error here because of the gzip compressed content
          service.error("MXOF","MESS",["Fail to integration ",responseObject['ErrorMessage']])

    I looked up Oracle REST API and found this link. There's some code here. See if you can use that. - https://docs.oracle.com/en/cloud/saas/marketing/responsys-develop/API/REST/Async/asyncApi-v1.3-performanceAnalytics-requests-requestId-get.htm

    ------------------------------
    MANU MAGANTI
    EMA Canada| Inc.
    OAKVILLE ON
    ------------------------------



  • 7.  RE: Decompress Gzip response content from External Rest API through Automation Script

    Posted Tue July 12, 2022 12:39 AM
    Hi @MANU MAGANTI,
    the service.error work as expected without the reference to responseObject['ErrorMessage'].
    Yes, I am trying to decompress it. Thanks for your link shared. I will try it.
    ​​

    ------------------------------
    Thi Minh Man Nguyen
    ------------------------------



  • 8.  RE: Decompress Gzip response content from External Rest API through Automation Script

    Posted Thu July 14, 2022 07:00 AM
    Hi @MANU MAGANTI,
    Thanks for your shared link.
    I am able to decompress the response. I'd like to share my python code here for someone who needs this.
    response = router.invoke(map, jsonStr)
    bufferedReader = BufferedReader(InputStreamReader(GZIPInputStream(ByteArrayInputStream(response))))​
    uncompressedData=""
    line = bufferedReader.readLine()
    while (line is not None):
       uncompressedData+=line
    line = bufferedReader.readLine()​
    logger.error(uncompressedData)


    ------------------------------
    Thi Minh Man Nguyen
    ------------------------------



  • 9.  RE: Decompress Gzip response content from External Rest API through Automation Script

    Posted Thu July 14, 2022 08:23 AM
    Excellent. Thanks for sharing the solution. :)

    ------------------------------
    MANU MAGANTI
    EMA Canada| Inc.
    OAKVILLE ON
    ------------------------------