#Add custom fields to HTTP return type

1 messages · Page 1 of 1 (latest)

junior mauve
#

I am trying to add a custom property to http return record type by doing following, public type Created record {| *http:Created; string id; |}; but when i add this new return type to the resource, it gives an error saying invalid resource method return type: expected anydata|http:Response|http:StatusCodeResponse|error, but found types:Created|types:InternalServerError(HTTP_102) new return type works if i remove the new id field.

wraith crow
#

Hi @junior mauve , the status code records in the http module are defined as closed records and it can only have the following fields:

  1. body - To represent the body of the response. This should be a anydata
  2. headers - To represent the headers of the response. This should be a map of map<string|int|boolean|string[]|int[]|boolean[]>
  3. media-type - To represent the Content-Type header. This should be a string
    (All of them are optional fields)

So adding new fields in the included record(Created) will make the structure different than http:StatusCodeResponse. Thats why you are getting the above error.

If you want to add the id as a part of the body of the response then you can do this[1]:

public type Created record {|
    *http:Created;
    record{string id;} body; // This will overwrite the body field
|};

If the above is not your usecase, can you elaborate on your usecase?

[1] https://ballerina.io/learn/by-example/http-send-different-status-codes-with-payload/

BBE on how to use the statusCode records in an HTTP resource methods.