Response field base on input Json field

Hi. For the response, can we set the field base on the input Json field?
Input Json contains = {
“personID”: “XXXXXXX”}

In the response, the field should also be named personID, and its value should match the value provided in the input JSON.

Note: l am using mock dynamic value for the other response fields for the same API.

You can achieve this by using custom scripts in the mock section to write code that dynamically sets the response field based on the input JSON.

I thought this is for the entire response. I can’t figure out what needs to be done if only 1 field is NOT using dynamic value and the rest is dynamic.

I have a similar requirement in which I want to set a response field to some specific input value based on a mock expectation, not always set for all request. Is it possible to achieve scripts based on mock expectations?

Yes, it’s supported. Please refer to this example: Mock scripts - Apidog Docs

// Get mock data from Smart Mock
var responseJson = $$.mockResponse.json();
// Modify the paged data from responseJson
// Set page as the page in request parameter
responseJson.page = $$.mockRequest.getParam(“page”);
// Write the modified json into $$.mockResponse
$$.mockResponse.setBody(responseJson);

But this will update for all requests, irrespective of any mock expectation. e.g. I have 2 mock expectations like if the body parameter has text “QA” i need to make the response id as input body parameter “Id” else response id should be input body parameter “returnId”

Input"
{
“text”:“QA|DEV”,
“id”:“123”,
“returnId”:“234”
}

Expectation 1: If text starts with DEV then
Response
{
“id”:“234”
}

Else
Response
{
“id”:“123”
}

Essentially having a mock script run for each mock expectation rather than only at global request level

This seems hardcoded and not dynamic .
Can we access faker.js dynamic value from the script?

Are you saying $$.mockResponse.json() is based on each field dynamic value?

Yes

Cool. I will try this at work tomorrow.
One more question.
Can the script override the field dynamic value to some value?

Yes, you can.

Nice

I am very excited to play around with Apidog with the various features.
I will definitely recommend APIdog to my colleagues once I am more familiar with Apidog

Is this something supported? I did not want to create another thread but if its needed to keep thing seperate I will create new one. ?

Please see a simple example screenshots below.
Screenshot 1) = mock script
Screenshot 2) = error faced
Screenshot 3) Endpoint response details.
Can you advise what the issue is?



You can modify the mock script like this:

var responseJson = $$.mockResponse.json();
//responseJson.personID = "S1234567B";
//responseJson.personID = "12";
$$.mockResponse.setBody({personID: "12" });

Still the same error with the provided Mock Script.


image.png

Please take note of the input JSON body has the same field name.

sage I had same error last week. Updating the apidog version fixed it