Returning a response code

Options
When we update data in a form, we would like to display a message indicating whether the update was successful or not. 

So, we need to return an http status code as well as the data of the record we updated.  How would I do that? 

I searched in the documentation. There was a screen shot with the message "The response for any Xano API is fully customizable."  

Thanks, Tony

Comments

  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    Hi Tony - you can add more than item to the response of your API endpoint. Just click the blue plus button in 3. Response of the API workflow to add additional responses.

    Your response could look something like this:
    [Screenshot_2021-06-29_23-11-43.png]
    Especially since, you will only get that response with a successful API run
  • Mr_English
    Options
      Thanks!
  • Unknown
    Options
     Hi Michael, do you hardcode the Status variable into the database so on recieving data from xano it checks against the variable Status for the 200 code. Or does xano return a http status code which we can get hold of in our application If so, where do we get the status code, it’s not part of the http request as far as I can see
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    For this use case I suggested the hardcode. But yes that is returned, for example, take one of your Xano endpoints and add it to an external API request function and run it. You'll be able to see the full response with the status and code
  • Felix Tan-2030210
    Options
    Hopping on this thread , is it possible to return a response code immediately, before letting other functions execute? 

    The consideration here being that certain external APIs have a response time limit (like Slack's 3s), while our workflows might be quite extensive and heavy.
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     currently the function stack must execute before returning a response. You can use the Return function to return a response once you have it but it will stop your function stack if there's any remaining logic. 
  • Felix Tan-2030210
    Options
     Is there any way in Xano to cater to this use case then? 

    For instance, just scheduling an API call in the future, but not now, so that the function stack can finish running and return a response. 

    Or enable multiple responses to be returned at different points in time? 
  • Victor Nihoul
    Victor Nihoul Member
    Options
     Also needed here for multiple use-cases   👍😀
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     I'd look at background tasks for scheduling API calls or logic in the future - they let you schedule things on frequency or in the future.

    You could always do something like add a specific time to a database table and always have your task start with a conditional so it only runs if that time is matched in the database. 
  • David Richardson
    Options
     I may be overthinking this but isn't hardcoding like {STATUS : value 200} as a return variable not allowing for a 400 response?  

    1) Does there need to be a conditional logic to ensure (say on a POST) that all values were updated properly?  

    2) And here is the question I thought you would answer, when "STATUS" is returned is that a JSON object?  Or something else?

    3) Might be kind of above my pay grade - but where ARE status codes generated?  Server? UNAUTHORIZED for instance.  Is it SOP to hard code those into API logic as a best practices kind of thing or does that happen "magically" by some "ghost in the machine" logic by the smart person who crafted the operating system?

    Thanks
  • Mr_English
    Options
       

    I think I have resolved this, at least for my solution, which is written in JavaScript.

    I read that an HTTP response accompanies a Fetch request, in the 'response'.  Here's my code:

    [res4.jpg]

    And here's the response:
    [res2.jpg]Note that I did not add any additional return variables:
    [res0.jpg]I have not tested this for errors.
    Disclaimer: I am not a pro developer!  If this is incorrect, please let me know.
  • Mr_English
    Options
    I can confirm this works.  New status code:[x20.jpg]
  • WMcCartney
    WMcCartney Member
    Options

    Hi All - I came across this thread and wanted to ask if anyone has found a better way to return a response code immediately, before letting other functions execute?

    For my usecase, Slack API requires a 200 response in <3000 milliseconds. I've found that using a Lambda Function (below) at the beginning of my Function Stack to return the 200 response code sometimes works, and sometimes does not. Any ideas would be appreciated.

    axios({
    method: 'post',
    url: 'https://www.xano.com/api:12345/apiname',
    data: {
    value: 'true',
    }
    });

    return "200 OK";

    Thanks!