Search code examples
httpmicroservicesjolie

Settings HTTP output header parameters working with aggregation


I am trying to change the contentType from the response of an aggregated operation, here is my example code.

interface MyAggregateInterface {
 RequestResponse:
 op1(typeOp1Request)(typeOp1Response)
}    
outputPort MyAggregatePort {
      Interfaces: MyAggregateInterface
    }

embedded {
      Jolie:
         "MyAggratedCode.ol" in MyAggregatePort
  }

 inputPort MyInputPortHttp {
    Protocol: http {
            .debug= 1;
            .debug.showContent =1;
            .format -> format;
            .contentType -> mime;
             .charset ="UTF-8";
            .default = "default";
            .compression = false
        }
        Location: "socket://localhost:8081"
        Interfaces: DefaultHttpInterface 
        Aggregates: MyAggregatePort
        }

I would like to change the return format for op1.


Solution

  • well I will try to answer your question

    we need to define your op1 response type

    type typeOp1Response:any{
      .format?:string
    }
    

    or if you prefer

    type typeOp1Response:undefined
    

    I personally prefer the first one so that you can decide the mime in the aggregated service

    Now you need to add a courier sessions

    courier MyInputPortHttp {
       [interface MyAggregateInterface( request )( response )]{
    
       forward( request )( response );
        if (is_defined(response.format)){
             mime = response.format;
             undef(response.format);
          }
        } 
    

    This implementation has a limitation that can return flat data in the root node Another way is to use the inputType to define your output format.

    type typeOp1Request:void{
      .otherParameter1:string
      .format?:string
    }
    

    then your courier

    courier MyInputPortHttp { [interface MyAggregateInterface( request )( response )]{

       forward( request )( response );
        if (request.format=="json"){
             mime = "application/json"
          };
        if (request.format=="xml"){
             mime = "application/xml"
          };
    
        } 
    

    Not sure if this answers your question