{
"ID": "2bdaad2e-e6f4-49d1-b737-e0276d8ccc03",
"name": "simple-config",
"metadata": {},
"secret_type": "Opaque",
"environment_id": "b1402013-b080-488d-858b-dadcb07a551a",
"data": null
}
Above JSON is the response from an HTTP client. We have an HTTP service and within that service, we call the above HTTP client and prepare a minimized response from the above JSON. The expected minimized response is,
{
"ID": "2bdaad2e-e6f4-49d1-b737-e0276d8ccc03",
"name": "simple-config"
}
Please find the relevant code below.
public type ResponseDto record {
string ID;
string name;
};
isolated resource function get () {
var httpClient = check getHttpClient();
string endpoint = string `/v1/external/api`;
var headers = utils:createHeaders(ctx);
json response = check httpClient->get(endpoint, headers, targetType = json);
ResponseDto res = check response.cloneWithType();
return res;
}
Still, I am getting full response and not getting the minimized response. How do I resolve this issue?
The ResponseDto
can hold additional fields other than ID
and name
. That is exactly what happens here as well. CloneWithType
does not loose data.
import ballerina/http;
public type ResponseDtoOpen record {
string ID;
string name;
};
public type ResponseDtoClosed record {|
string ID;
string name;
|};
service / on new http:Listener(9090) {
isolated resource function get foo() returns ResponseDtoClosed|error {
http:Client httpClient = check new ("http://localhost:9090");
map<string> headers = {};
ResponseDtoOpen response = check httpClient->/v1/'external/api(headers = headers);
ResponseDtoClosed res = {ID: response.ID, name: response.name};
return res;
}
};
In the above code, we are mapping data with the required fields.