How to send proper http response from web API?

External system calling appian web api with some json value. Now I have to put those data into database and send response accordingly.

Response code to send: 204 for success, 207 for partial success(eg. when 5 out of 10 records added successfully), 400 (bad gateway), 500, 502 and 504

 

How to configure it and test it in appian itself?

  Discussion posts and replies are publicly visible

Parents
  • Hi Sudipta,

    Are you using process model to initiate the process and put data into database or directly write to data store from web API?

    While creating WebAPI, you can see both options. In your case i think better to use process model to evaluate results such as partial success 207, 500 etc.

    In general, you can use following,

    onSuccess: a!httpResponse(

         statusCode: 200,

         headers: {

           a!httpHeader(

             name: "Content-Type",

             value: "application/json"

           )

         },

         body: a!toJson(

           {

             status: "Success",

             message: "Data insertion successful.",

             caseNumber: index(

               fv!processInfo.pv,

               "result",

               ""

             )

           }

         )

       ),

       onError: a!httpResponse(

         statusCode: 500,

         headers: {

           a!httpHeader(

             name: "Content-Type",

             value: "application/json"

           )

         },

         body: a!toJson(

           {

             status: "Error",

             message: "There is an error starting the process"

           }

         )

    In your case, on success will contain conditional a!httpResponse e.g. for Success & Partial Success. You need to return result from process.

    Similarly, you need to conditionally add response for onError as well.

  • How can I add conditional response in onError or in onSuccess? Can you give an example or codeSnippet?
  • Hi Sudipta,

    When you execute process from Web API, you have more control to respond back particular status code based on "fv!processInfo". say, based on some processing within your process, you can set PV and based on PV value you can set status code e.g.
    onSuccess: a!httpResponse(
    statusCode: if(
    index(
    fv!processInfo.pv,
    "status",
    ""
    ) = "PARTIAL",
    204,
    200
    )

    As Alok mentioned, first we need to understand do you really require overriding default statuses?
    You need to understand that we usually do that to provide more information in the response which otherwise not available. For e.g. in case of 500 which is internal server error, we really want to know what went wrong. Similarly, if 200 for success, we may want to return result as a response.
    I don't think we have control on all the status codes. for e.g. for Bad gateway, Web API will respond back with that code if it encounters that error, but, what you want to add in that response ? Similarly, for client side errors 4xx e.g. 401 Unauthorized. It will come back as response anyways, we don't need to do anything on that.
    i hope it clarifies you.

Reply
  • Hi Sudipta,

    When you execute process from Web API, you have more control to respond back particular status code based on "fv!processInfo". say, based on some processing within your process, you can set PV and based on PV value you can set status code e.g.
    onSuccess: a!httpResponse(
    statusCode: if(
    index(
    fv!processInfo.pv,
    "status",
    ""
    ) = "PARTIAL",
    204,
    200
    )

    As Alok mentioned, first we need to understand do you really require overriding default statuses?
    You need to understand that we usually do that to provide more information in the response which otherwise not available. For e.g. in case of 500 which is internal server error, we really want to know what went wrong. Similarly, if 200 for success, we may want to return result as a response.
    I don't think we have control on all the status codes. for e.g. for Bad gateway, Web API will respond back with that code if it encounters that error, but, what you want to add in that response ? Similarly, for client side errors 4xx e.g. 401 Unauthorized. It will come back as response anyways, we don't need to do anything on that.
    i hope it clarifies you.

Children
No Data