'Getting Invalid Response from API . - Groovy Scripting

I am trying a POST method in groovy which basically creates a Issue in Jira . I am getting invalid response from the API . However in postman I could able to get the response . I have searched few questions in stackoverflow where they are recommending to set the Accept-Encoding to "" . I tried out different methods but it didnt work .

Code :

import javax.net.ssl.*;
import java.security.cert.*;
import groovy.json.JsonBuilder;
import groovy.json.JsonOutput;
import groovy.json.JsonSlurperClassic;
import groovy.json.StreamingJsonBuilder;


def jiraurl = "xxxxx";
def netProxyAddr =  "xxxx";
def netProxyPort = 80;




try {
    // NOTE: Adjust the following as needed
    
    

    def uri = jiraurl + "/issue";   

     def jiraTicketMap = [:];
     
     jiraTicketMap["fields"] = [:];
     jiraTicketMap["fields"]["project"]= [:];
     jiraTicketMap["fields"]["project"]["id"] = "12412"
     jiraTicketMap["fields"]["summary"] = "Test ticket for API";
     jiraTicketMap["fields"]["assignee"] = [:];
     jiraTicketMap["fields"]["assignee"]["name"] = "*********";
     jiraTicketMap["fields"]["customfield_10700"] = [:];
     jiraTicketMap["fields"]["customfield_10700"]["value"] = "Normal";
     jiraTicketMap["fields"]["customfield_16901"] = "nothing impacted";
     jiraTicketMap["fields"]["customfield_16900"] = "testing ";
     jiraTicketMap["fields"]["customfield_10710"] = "removing the URL" ;
     jiraTicketMap["fields"]["customfield_10711"] = "********";
     jiraTicketMap["fields"]["customfield_16902"] = "descriptiong";
     jiraTicketMap["fields"]["customfield_10702"] = [:];
     jiraTicketMap["fields"]["customfield_10702"]["value"] ="Low";
     jiraTicketMap["fields"]["customfield_10900"] = [:];
     jiraTicketMap["fields"]["customfield_10900"]["value"] = "Yes";
     jiraTicketMap["fields"]["customfield_10703"] = [:];
     jiraTicketMap["fields"]["customfield_10703"]["value"] = "Low";
     jiraTicketMap["fields"]["customfield_10705"] = "2022-03-19T10:29:29.908+1100";
     jiraTicketMap["fields"]["customfield_10706"] = "2022-03-20T10:29:29.908+1100";
     jiraTicketMap["fields"]["customfield_10707"] = "testing done";
     jiraTicketMap["fields"]["customfield_10708"] = "Implemtation test done for zscaler";
     jiraTicketMap["fields"]["customfield_10709"] = "post implention done";
     jiraTicketMap["fields"]["issuetype"] = [:];
     jiraTicketMap["fields"]["issuetype"]["id"]= "10200";
     jiraTicketMap["fields"]["labels"] = ["test"];
     jiraTicketMap["fields"]["customfield_21400"] = "Test Environment is required.";
     jiraTicketMap["fields"]["customfield_21500"] = "Back-Out Test Details is required";
     
     
     
     
     
       DEBUG.println("DEBUG: jiraTicketMap is: ${jiraTicketMap}");
       def payloadData = StringUtils.mapToJson(jiraTicketMap);
       DEBUG.println("DEBUG: Payload is: ${payloadData}");
     
    
    
       DEBUG.println("DEBUG: PayloadData is: ${payloadData}");
       DEBUG.println("DEBUG: PayloadData type is:"+payloadData.getClass());

    
    netProxyPort = netProxyPort.toInteger();
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(netProxyAddr, netProxyPort));
    
    
    def authString = "*********".getBytes().encodeBase64().toString()
    
    def url = new URL(uri);
    // NOTE: The following removes all checks against SSL certificates
    disableSSL();
    def conn = url.openConnection(proxy);
    conn.doOutput = true;
    
    // Build the Credentials and HTTP headers
    conn.setRequestProperty("Authorization", "Basic ***********");
    conn.setRequestProperty("Content-type", "application/JSON;charset=UTF-8");
    conn.setRequestProperty("Accept","*/*");
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    
    
    //BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF8"));
    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(),"UTF-8")
    writer?.write(payloadData);
    writer?.flush();
    writer?.close();
    
    if(conn.getResponseCode() == 201 || conn.getResponseCode() == 200 ) {
        InputStream responseStream = conn.getInputStream();
        result = responseStream.getText("UTF-8");
        
        DEBUG.println("DEBUG:>>>>>>>>>>>>> SUccessfully Posted Data to Jira");
        // NOTE: If you need to get the response header, use below
        //INPUTS.HEADERS = conn.getHeaderFields();
        responseStream.close();
    }
    else {
        INPUTS.RSEXCEPTION = "Response code was " + conn.getResponseCode();
        // Attempt to parse error stream.
        InputStream responseStream = conn.getErrorStream();
        result = responseStream?.getText("UTF-8");
        responseStream?.close();
    }
    
}
catch (Exception e) {
    //INPUTS.RSEXCEPTION = e.getMessage();
    result = e.getMessage();
    result += "\n" + e.getClass() + ": " + e.getMessage();
    for (trace in e.getStackTrace())
    {
        result += "\n\t" + trace;
    }
}

return result;

Response :

Response from Postman and local code editor

{
    "id": "12212",
    "key": "WCM-2211",
    "self": "https://********/rest/api/2/issue/2496228"
}

Response from Vendor Tool:

�4̱@0��w��^BV��\z(M��J"��u1���?`4tPȶ.���S���7��0�%���q7A    M�X����xv…qD�h�O��~��NCe

Response Headers

[X-AREQUESTID:[533x1208409x1], null:[HTTP/1.1 201 Created], Server:[Web Server], X-Content-Type-Options:[nosniff], Connection:[close], X-ANODEID:[node1], Date:[Thu, 12 May 2022 07:53:25 GMT], X-Frame-Options:[SAMEORIGIN], Referrer-Policy:[no-referrer-when-downgrade], Strict-Transport-Security:[max-age=15768000], Cache-Control:[no-cache, no-store, no-transform], Content-Security-Policy:[frame-ancestors 'self'], X-AUSERNAME:[SVC], Content-Encoding:[gzip], X-ASESSIONID:[1l2vszx], Set-Cookie:[akaalb_jira_dev_alb=~op=jira_dev_lb:jira-dev-ld6|~rv=95~m=jia-dev-ld6:0|~os=287df636055edb4e278283~id=de0d2567c76bf80374af7c583f; path=/; HttpOnly; Secure; SameSite=None, JESSIONID=2920944044.37151.0000; path=/; Httponly; Secure, atlassian.xsrf.token=BZXB-Z179-LJQD-6WAV_9b298f0387a7c68a8652e963b69e4_lin; Path=/; Secure, JSESSIONID=27C93900AA8B780AA44C2861F73; Path=/; Secure; HttpOnly], Feature-Policy:[midi 'none'], Vary:[Accept-Encoding, User-Agent], X-XSS-Protection:[1; mode=block], X-Seraph-LoginReason:[OK], X-Powered-By:[Web Server], Content-Type:[application/json;charset=UTF-8]]

Solution from @dagget : // use the below code when reading it from input stream

   accept-encoding : identity 
    responseStream = new java.util.zip.GZIPInputStream(responseStream)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source