Search code examples
javagroovyhashmaphttpresponsekatalon-studio

Convert a Map value to String


I am trying to convert a map value to String. I tried toString() method but it still returns an Object instead of String

response = WS.sendRequest(findTestObject('api/test/TD-4_01_01-Valid'))

Map parsed = response.getHeaderFields()

String messageId = parsed.get('x-message-id').toString();

println messageId

Actual Output:

[C5yZC5hcy5sb2NhbC0xMjgyNi05MzE1LTE=] 

Expected Output:

C5yZC5hcy5sb2NhbC0xMjgyNi05MzE1LTE=

Solution

  • ResponseObject#getHeaderFields returns a Map of String keys to a List of String objects as vales. You simply need to get the List of String objects for the key x-message-id and since you expect it to return a single result, find any.

    ResponseObject response = WS.sendRequest(findTestObject('api/test/TD-4_01_01-Valid'));
    
    Map<String, List<String>> parsed = response.getHeaderFields();
    
    List<String> messageIdList = parsed.get("x-message-id");
    
    String messageId = messageIdList.stream().findAny().orElseThrow(IllegalStateException::new);