Forum Discussion

Buschfunk's avatar
Buschfunk
Frequent Contributor
15 years ago

How to use property "Encode Attachments" correctly?

Hello,

I have set up a webservice mock which sends responses with dynamic content read from a database. Some of the response nodes shall contain base64-encoded binary data, e.g. images.

For debugging purposes, I use the following statement which works fine. It shows the base64 representation of the image.

log.info(sqlResult['IMAGE'].encodeBase64())


If, however, I use the SQL result as a context property for further usage in the response it is not handled correctly, i.e. instead of its base64 representation only a string like [B@156... is used.

The property "Encode attachments" is set to true.

In the Groovy script:
context.image = sqlResult['IMAGE']


In the response:
${image}

3 Replies

  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    If you are using the encodeBase64() method in your log output, why are you not using the same when assigning to the context property, like this:

    context.image = sqlResult['IMAGE'].encodeBase64()


    The property expansion will do a toString() conversion; if you do

    log.info sqlResult['IMAGE'].toString()


    do you get the base64 representation or [B@156?

    Just my 2 cents.
  • Buschfunk's avatar
    Buschfunk
    Frequent Contributor
    Hey,

    Thanks for your reply.

    M McDonald wrote:

    If you are using the encodeBase64() method in your log output, why are you not using the same when assigning to the context property, like this:

    context.image = sqlResult['IMAGE'].encodeBase64()



    Sure, this would work fine. Actually, it does But the problem is the following: All node values of the mock response are taken from a database. As this list of parameters is pretty long, I'm using an each-statement to iterate through the parameters. And as this list is dynamical, I can't tell in advance if a parameter is a binary (which needs to be base64 encoded) or if it is just a regular string.

    Maybe there's a Groovy method to determine if an object is binary?

    Cheers,
    Robert
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Can you convert the value for that field directly in the SQL statement? That way all the remaining fields are as-is.