Posting to REST API

Posting to REST API

  

Hi All, 


So when I try to make a post to an API the request has to be in JSON. I tried to enter the request as a JSON string but it complains that the type is not correct. What is the correct way to convert the Text to JSON?


Regards

Hi Lee,

From where are you trying to post to the REST API?

Did you consume the REST API in OutSystems?

Do you see the REST API as an action in Service Studio?

If this is the case, then you can just fill in the values in the inputs and you can igore the JSON.

Maybe it's best if you show a screenshot of what you are trying to accomplish or could you share your module?

Kind regards,

Remco Dekkinga

Hi Remco,

I am attaching a screenshot of my screen. The problem is I am calling a POST type REST api which has request parameters as:

{
"User":"123456",
"Password":"xyz@123"
}

When I write these parameters in the request block of Run Server Action on the right (which is red colored now), it gives the error "PostLoginRequest data type required instead of Text" (PostLoginRequest is the name of my API). Kindly suggest how it can be solved.


Kind Regards,

Charul Mathur

Solution

Hi Charul,

The Request is a structure with two attributes, not a piece of text where you can just copy/paste JSON.

To make it work:

  1. Create a local variable named RequestStruct
  2. Put an Assign widget in the OnLoginClick action just before the PostLogin action.
  3. Place the following assignments in the Assign widget:
    1. RequestStruct.User = "123456"
    2. RequestStruct.Password = "xyz@123"
  4. Place the RequestStruct variable in the Request input
  5. Publish your module and you're done.

Addition to make it maintainable:

Create two Site Properties for the User and Password and assign the site properties instead of the hard-coded values. This way you can change the username/password in ServiceCenter without the need to republish your application.

Kind regards,

Remco Dekkinga

Solution

Remco Dekkinga wrote:

Hi Charul,

The Request is a structure with two attributes, not a piece of text where you can just copy/paste JSON.

To make it work:

  1. Create a local variable named RequestStruct
  2. Put an Assign widget in the OnLoginClick action just before the PostLogin action.
  3. Place the following assignments in the Assign widget:
    1. RequestStruct.User = "123456"
    2. RequestStruct.Password = "xyz@123"
  4. Place the RequestStruct variable in the Request input
  5. Publish your module and you're done.

Addition to make it maintainable:

Create two Site Properties for the User and Password and assign the site properties instead of the hard-coded values. This way you can change the username/password in ServiceCenter without the need to republish your application.

Kind regards,

Remco Dekkinga

Hi Remco,

Thanks a lot for your quick response. It solved my problem. 

Sorry im newbie here...

How to create a local variable - RequestStruct with two sub variable like RequestStruct.User and RequestStruct.Password ?



Charul Mathur wrote:

Remco Dekkinga wrote:

Hi Charul,

The Request is a structure with two attributes, not a piece of text where you can just copy/paste JSON.

To make it work:

  1. Create a local variable named RequestStruct
  2. Put an Assign widget in the OnLoginClick action just before the PostLogin action.
  3. Place the following assignments in the Assign widget:
    1. RequestStruct.User = "123456"
    2. RequestStruct.Password = "xyz@123"
  4. Place the RequestStruct variable in the Request input
  5. Publish your module and you're done.

Addition to make it maintainable:

Create two Site Properties for the User and Password and assign the site properties instead of the hard-coded values. This way you can change the username/password in ServiceCenter without the need to republish your application.

Kind regards,

Remco Dekkinga

Hi Remco,

Thanks a lot for your quick response. It solved my problem. 

Hi Charul,

How to create a local variable - RequestStruct with two sub variable like RequestStruct.User and RequestStruct.Password ?


Hi Michael,

By replying to a thread that had already got an answer, you won’t be noticed. Try to create a new question on the forum next time. 

To answer your question: on the Data tab, you can create entities and structures. Structures are similar to entities, except that they are not stored in the database and the values only exist in the scope of the variable that you assign the structure to. 

In order to create the above structure, create a structure named Request and add two attributes: Username and Password. 

The create the variable inside your Rest method and name it RequestStruct. Check if it’s of type Request structure otherwise change the type by hand from Text to Request structure. 

This should fix your problem.

Ps  when working with passwords, make sure you keep them safe and preferably send the encoded.

Kind regards,

Remco Dekkinga