0
votes

I need to pass the Date format variable data from Bean shell processor to http request body

Below is my code and json where I passed variable data but it is not working

import java.text.SimpleDateFormat;
import java.util.Date;

Date enrolmentDate = new Date();
enrolmentDate.setDate(enrolmentDate.getDate());//+ ${__Random(1,50,)});
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
String formattedDate = df.format(enrolmentDate);
vars.put("StartDate",formattedDate);
log.info("########################"+formattedDate);

Below is the Http Request Body data

{
"articleId":""${ArticleId}",
"startDate":"${formattedDate}",
"endDate":"${Carttodates}"
}

When i run it Start date and end date is shown as ${formattedDate}, what will be the solution?

and in my JSON body data i want to send Start and End Date like "27/05/2019 14:34 "

Below is the Request I got

PUT data:
{
"articleId":"7694b207-936b-40b9-9c80-4b8097e67da1",
"startDate":"${formattedDate}",
"endDate":"${Carttodates}"
}
2

2 Answers

0
votes

Change your request body to

{
"articleId":""${ArticleId}",
"startDate":"${StartDate}",
"endDate":"${Carttodates}"
}

The reason why this is required is because you are storing the date in "StartDate" variable in beanshell. Hence, you should use "StartDate" to access the value later in HTTP. The other option is to store the value in "formattedDate" variable in beanshell and then you do not need to change it in HTTP request body.

0
votes

You need to put formattedDate as the variable name also:

vars.put("formattedDate", formattedDate);