2

Does anyone know how to change response HTTP Status Code in a procedure type REST in Genexus?

I generated an api rest and it is always responding Http code 200, and I need to change the response to 400 when there is an error.

ferlrp
  • 21
  • 1

1 Answers1

4

API object is available in Genexus 17 for Net and Net Core generators, and GeneXus 17 Upgrade 2 for Java generator. API Object allows you to customize the HTTP Status Code through a predefined variable called &RestCode. Here the wiki for the API Object. If you're using an older version, there is a way to solve the problem writting external code directly in the PRC as explained here.

Depending wich generator are you using the external code is something like this:

.Net generator:

CSHARP [!&httresponse!].Response.StatusCode = 406;

Java generator:

&statusCode = 200
java context.getHttpContext().getResponse().setStatus([!&statusCode!]);

Ok, that code doesn't seems to work in GeneXus Evolution 3 when the PRC is exposed as REST. Doing a little research in the Java Generator, you can modify the StatusCode this way:

enter image description here

And the rules are:

enter image description here

It's a very simple PRC exposed as REST who takes a number as input and generates a String with that number. Don't know exactly how to do that in Net generator.

  • Thanks for your reply. I'am using GX Ev3. I don't understand where to put that code, because I'm just generating a procedure with property Rest in True. Could you please explain me where I can use that code – ferlrp Sep 17 '21 at 15:36
  • 1
    Ok, edited my reply adding further information about your specific case, I've found a solution for Java generator but my lack of knowledge in C# prevents doing the same in such generator. – Leandro Minatel Sep 18 '21 at 22:19
  • 1
    Leandro very clear your explanation, I tried and it is working. Thank you so much! – ferlrp Sep 20 '21 at 17:24