# How to pass base64 encoded variable value into .Json file

**URL:** <https://community.gatling.io/t/how-to-pass-base64-encoded-variable-value-into-json-file/8212>\
**Category:** Gatling (Open-Source)\
**Created:** [August 17, 2023, 5:03am UTC](https://community.gatling.io/t/how-to-pass-base64-encoded-variable-value-into-json-file/8212 "2023-08-17T05:03:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raj007](https://avatars.discourse-cdn.com/v4/letter/r/5fc32e/32.png) [@Raj007](https://community.gatling.io/u/Raj007)\
**Post date:** [August 17, 2023, 5:03am UTC](https://community.gatling.io/t/how-to-pass-base64-encoded-variable-value-into-json-file/8212/1 "2023-08-17T05:03:33Z")

</div>

Hello,

I’m getting “400” error response from Post request while passing base64 encoded variable value to .json file. Below is code snippet.

val encodedMessage = Base64.getEncoder.encode(sourceCodeBytes)  
val base64Msg = new String(encodedMessage)

In post request:  
.body(ElFIileBody(“apipackage1/recordedsimulationapi/0000\_request.json”))

In 0000\_request.json file:  
{“messages”:[{“data”:“#{base64Msg}”,“attributes”:{“GaSell”: “CTY.DK”,“contentMsgId”:“1”}}]}

Can anyone pls help on this issue.

Appreciate your quick response.

Thanks.

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [August 17, 2023, 5:41am UTC](https://community.gatling.io/t/how-to-pass-base64-encoded-variable-value-into-json-file/8212/2 "2023-08-17T05:41:57Z")

</div>

You have to prepare the encoded String upfront with the Session API. Please read the [documentation](https://gatling.io/docs/gatling/reference/current/core/session/session_api/).

---

<div class="post-metadata">

**Author:** ![Raj007](https://avatars.discourse-cdn.com/v4/letter/r/5fc32e/32.png) [@Raj007](https://community.gatling.io/u/Raj007)\
**Post date:** [August 17, 2023, 6:29am UTC](https://community.gatling.io/t/how-to-pass-base64-encoded-variable-value-into-json-file/8212/3 "2023-08-17T06:29:02Z")

</div>

Thanks for your response Stephane!

I tried it as per the documentation. May be i’s wrong. I’m completely new to Gatling.

Can you pls send code snippet for how to prepare the encoded String upfront with the Session API.

Appreciate for your quick response.

Thanks.

---

<div class="post-metadata">

**Author:** ![Raj007](https://avatars.discourse-cdn.com/v4/letter/r/5fc32e/32.png) [@Raj007](https://community.gatling.io/u/Raj007)\
**Post date:** [August 17, 2023, 11:39am UTC](https://community.gatling.io/t/how-to-pass-base64-encoded-variable-value-into-json-file/8212/4 "2023-08-17T11:39:45Z")

</div>

Hi Stephane,

Still getting 400 error while sending payload to GCP pub/sub topic.  
Below is http response from post request. In body, i’m sending correct base64 encoded values. But still giving bad request error. Can you pls let me know how to send the payload to GCP pub/sub topic.

body:ByteArrayRequestBody{contentType=‘application/json’, content (Base64)=eyJtZX

HTTP response:  
status:  
400 Bad Request  
headers:  
Vary: X-Origin  
Vary: Referer  
Content-Type: application/json; charset=UTF-8  
Date: Thu, 17 Aug 2023 10:59:13 GMT  
Server: ESF  
Cache-Control: private  
X-XSS-Protection: 0  
X-Frame-Options: SAMEORIGIN  
X-Content-Type-Options: nosniff  
Alt-Svc: h3=“:443”; ma=2592000,h3-29=“:443”; ma=2592000  
Accept-Ranges: none  
Vary: Origin,Accept-Encoding  
Transfer-Encoding: chunked

body:  
{  
“error”: {  
“code”: 400,  
“message”: “Invalid value at ‘messages[0].data’ (TYPE\_BYTES), Base64 decoding failed for "#{xyz}"”,  
“status”: “INVALID\_ARGUMENT”,  
“details”: [  
{  
“@type”: “[type.googleapis.com/google.rpc.BadRequest](http://type.googleapis.com/google.rpc.BadRequest)”,  
“fieldViolations”: [  
{  
“field”: “messages[0].data”,  
“description”: “Invalid value at ‘messages[0].data’ (TYPE\_BYTES), Base64 decoding failed for "#{xyz}"”  
}  
]  
}  
]  
}  
}

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b148e0742de35c6280726cabf3ecd3de2d320f63.png) [@system](https://community.gatling.io/u/system)\
**Post date:** [September 16, 2023, 11:40am UTC](https://community.gatling.io/t/how-to-pass-base64-encoded-variable-value-into-json-file/8212/5 "2023-09-16T11:40:20Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
