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

  • Please take a look at the Web API documentation: docs.appian.com/.../Designing_Web_APIs.html. When configuring the Web API, you can specify which HTTP response code to send.
  • You can use
    onSuccess: a!httpResponse()
    onErroor: a!httpResponse()

    Then configure the parameters for the httpResponse function passing in your status cde
  • 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.

  • 0
    Certified Lead Developer

    Hi  are there any specific reason behind overriding the default HTTP Status code?

    As you might be aware that, status 204 stands for "No content" where as  you want to return 204 for success. I agreed, http status 2xx indicates success but still each code indicates different statuses. E.g.

    200: OK

    201: Created

    202: Accepted

    203: Non-Authoritative Information

    204: No Content  etc...

    Generally when you are working with Web API then API takes care of the http responses e.g. (404, 500, 201, 200 etc..) however as per our requirement we can customise few responses (but not all, e.g. 404) by using a!httpResponse() function.

    Also, when you are persisting the data into the db using WEB API , I recommend using process model instead of smart service functions, because if it's a process, you will have better trace of the data and better control to debug the issues occued (if any).

  • 0
    Certified Lead Developer
    in reply to sekars7177

     The above mention URL explains the basics about, how to create and configure a WEB-API in Appian. May i know, what do you me to refer here please?

  • The web api gives status code 200 and 500 by default in onSuccess and onError. But how I configure 207, 400, 502 and 504?

     

  • 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.