Consume REST API - How to consume REST API and search?

Hi! I'm consuming a rest api in my application. I'm finding difficult implementing a search through the api and showing the results in a table record? Any ideas how can this be achieved?


Cheers.

Hi Paulo, 

Can you give a bit more detail on the API you are consuming?


The basics would be, in your screen you have a local variable “SearchKeyword” associated to the input widget and a Search Button. When the user writes something on the input and clicks the Search button, the SearchKeyword local variable should be used as input parameter of the API Method for search.


There other concerns here but let’s start with this :)

If the API (for some devilish reason) does not support search then you'll have to fetch all records, this will give you a List of some Structure, and then you can use your Filter actions to get what you want.

Cristiana Umbelino wrote:

Hi Paulo, 

Can you give a bit more detail on the API you are consuming?


The basics would be, in your screen you have a local variable “SearchKeyword” associated to the input widget and a Search Button. When the user writes something on the input and clicks the Search button, the SearchKeyword local variable should be used as input parameter of the API Method for search.


There other concerns here but let’s start with this :)

Hi Cristiana.

The API that I'm consuming is the itsyourskills api.

The keyword is the input parameter in the url "https://skills.itsyourskills.com/api/v2/skill-search?q={Keyword}" in order to query by it. I had implemented the session variable in the input search box and also in the keyword parameter of the server action of the api in the preparation. At the search button, I have an ajax submit method that will do a ajax refresh of the list.


Cheers.


Manish Jawla wrote:

Hi Paulo,

check the below links:

https://www.youtube.com/watch?v=DiKHR7Qe1Es


https://www.google.com/search?q=consume+rest+api+in+outsytems&rlz=1C1GCEA_enGB874GB874&oq=consume+rest+api+in+outsytems&aqs=chrome..69i57j0.6764j0j8&sourceid=chrome&ie=UTF-8#kpvalbx=_8m26XuL4FsaT8gLfv56oBg43


Hi Manish. Thank you for your reply. Actually I saw that video before and I did an extensive search trying to find an answer to my problem but it didn't help :(


Cheers.

Mariano Picco wrote:

If the API (for some devilish reason) does not support search then you'll have to fetch all records, this will give you a List of some Structure, and then you can use your Filter actions to get what you want.

Hi Mariano.

Thanks for your reply.

I do the query in the url with the keyword (https://skills.itsyourskills.com/api/v2/skill-search?q={Keyword}).

This is the API.


Cheers.



When you Test your api call with a custom keyword, does it return a filtered list like you expect it to? 

When you're debugging the call, do you see the correct word is being sent out with the api call?

You might have to turn on API logging on your service center configurations so you can see the call's headers

Paulo Solipa wrote:

Hi! I'm consuming a rest api in my application. I'm finding difficult implementing a search through the api and showing the results in a table record? Any ideas how can this be achieved?


Cheers.

Hi,


Consume API is easy in OutSystems, can you please more details.


Mariano Picco wrote:

When you Test your api call with a custom keyword, does it return a filtered list like you expect it to? 

When you're debugging the call, do you see the correct word is being sent out with the api call?

You might have to turn on API logging on your service center configurations so you can see the call's headers


If I hardcoded, yes it works. Otherwise it doesn't. Simply doesn't appear any record or something like this appears in the service center logs. I don't know if it's something I'm doing wrong in the search or something else.

Paulo Solipa wrote:

Cristiana Umbelino wrote:

Hi Paulo, 

Can you give a bit more detail on the API you are consuming?


The basics would be, in your screen you have a local variable “SearchKeyword” associated to the input widget and a Search Button. When the user writes something on the input and clicks the Search button, the SearchKeyword local variable should be used as input parameter of the API Method for search.


There other concerns here but let’s start with this :)

Hi Cristiana.

The API that I'm consuming is the itsyourskills api.

The keyword is the input parameter in the url "https://skills.itsyourskills.com/api/v2/skill-search?q={Keyword}" in order to query by it. I had implemented the session variable in the input search box and also in the keyword parameter of the server action of the api in the preparation. At the search button, I have an ajax submit method that will do a ajax refresh of the list.


Cheers.



Hi Apulo,

ajax submit will not refresh your list, if you want to implement the search functionality offered by this api, then i hope you followed the below steps:

1. test the api with search parameter whether it's working fine or not and you are getting desired result?

2. On your screen where you have placed the button you are calling the search api by using your session variable and assigning the result of your search api back to the source of tablerecord widget.?

I hope this helps.

JitendraYadav wrote:

Paulo Solipa wrote:

Hi! I'm consuming a rest api in my application. I'm finding difficult implementing a search through the api and showing the results in a table record? Any ideas how can this be achieved?


Cheers.

Hi,


Consume API is easy in OutSystems, can you please more details.


Hi Jitendra.

Thank you for your reply.

I already consumed the api. My problem is that I can't do a search like we do with the aggregates.

With an search input, we search through the aggregate and get the results in a list records.

That's what I intent to do with the REST API but I'm not being able to.


Manish Jawla wrote:


Hi Apulo,

ajax submit will not refresh your list, if you want to implement the search functionality offered by this api, then i hope you followed the below steps:

1. test the api with search parameter whether it's working fine or not and you are getting desired result?

2. On your screen where you have placed the button you are calling the search api by using your session variable and assigning the result of your search api back to the source of tablerecord widget.?

I hope this helps.

Hi Manish.

Yes I did those steps. This is my search button screen action, that makes an ajax refresh to the list records.



Paulo Solipa wrote:

JitendraYadav wrote:

Paulo Solipa wrote:

Hi! I'm consuming a rest api in my application. I'm finding difficult implementing a search through the api and showing the results in a table record? Any ideas how can this be achieved?


Cheers.

Hi,


Consume API is easy in OutSystems, can you please more details.


Hi Jitendra.

Thank you for your reply.

I already consumed the api. My problem is that I can't do a search like we do with the aggregates.

With an search input, we search through the aggregate and get the results in a list records.

That's what I intent to do with the REST API but I'm not being able to.


Ok Cool,


Are you passing that search string as an input parameter to API call?



JitendraYadav wrote:

Ok Cool,


Are you passing that search string as an input parameter to API call?


Hi Jitendra.

Yes, I am.

Cheers.


Paulo Solipa wrote:

JitendraYadav wrote:

Paulo Solipa wrote:

JitendraYadav wrote:

Paulo Solipa wrote:

Hi! I'm consuming a rest api in my application. I'm finding difficult implementing a search through the api and showing the results in a table record? Any ideas how can this be achieved?


Cheers.

Hi,


Consume API is easy in OutSystems, can you please more details.


Hi Jitendra.

Thank you for your reply.

I already consumed the api. My problem is that I can't do a search like we do with the aggregates.

With an search input, we search through the aggregate and get the results in a list records.

That's what I intent to do with the REST API but I'm not being able to.


Ok Cool,


Are you passing that search string as an input parameter to API call?



Hi Jitendra.

Yes, I am.

Cheers.



Ok Great can you please share the snap of that call

Paulo Solipa wrote:

Manish Jawla wrote:

Paulo Solipa wrote:

Cristiana Umbelino wrote:

Hi Paulo, 

Can you give a bit more detail on the API you are consuming?


The basics would be, in your screen you have a local variable “SearchKeyword” associated to the input widget and a Search Button. When the user writes something on the input and clicks the Search button, the SearchKeyword local variable should be used as input parameter of the API Method for search.


There other concerns here but let’s start with this :)

Hi Cristiana.

The API that I'm consuming is the itsyourskills api.

The keyword is the input parameter in the url "https://skills.itsyourskills.com/api/v2/skill-search?q={Keyword}" in order to query by it. I had implemented the session variable in the input search box and also in the keyword parameter of the server action of the api in the preparation. At the search button, I have an ajax submit method that will do a ajax refresh of the list.


Cheers.



Hi Apulo,

ajax submit will not refresh your list, if you want to implement the search functionality offered by this api, then i hope you followed the below steps:

1. test the api with search parameter whether it's working fine or not and you are getting desired result?

2. On your screen where you have placed the button you are calling the search api by using your session variable and assigning the result of your search api back to the source of tablerecord widget.?

I hope this helps.

Hi Manish.

Yes I did those steps. This is my search button screen action, that makes an ajax refresh to the list records.




Hi Paulo,

I think you are confused here, aggregates are database entity and their result got stored in db but in case of api you getting result on the fly and it is not stored anywhere.

In your search code you again need to call the seach api and pass the parameter with all the token n all and fetch the result and assign it back to your grid data source, by simply calling the ajax action your result will not get filtered.

Manish Jawla wrote:


Hi Paulo,

I think you are confused here, aggregates are database entity and their result got stored in db but in case of api you getting result on the fly and it is not stored anywhere.

In your search code you again need to call the seach api and pass the parameter with all the token n all and fetch the result and assign it back to your grid data source, by simply calling the ajax action your result will not get filtered.

Hi Manish. Yes I know that ;) I was giving an example so I can be better understood.

I'm passing the response in an expression like this:


Hi Paulo,

Your problem is solved or not? i means did you manage to get the filtered result or not through api now?

I hope if you are again calling your api then you will definitely get the desired result.


JitendraYadav wrote:

Ok Great can you please share the snap of that call



I think that's all I have.

Cheers.

Manish Jawla wrote:

Hi Paulo,

Your problem is solved or not? i means did you manage to get the filtered result or not through api now?

I hope if you are again calling your api then you will definitely get the desired result.

Hi Manish.

No it's still not working.


Hi Paulo,

what i understood is you are calling one api on the preparation of you page and have some question?

- are you using the response of that api from preparation result? if not why you are calling the api on preparation?

- recommendation- why don't you call the api on search button action and have some local variable to hold the response of your api, so that in the beginning of the page you can show blank placeholder with local variable but on search button you can call api and assign the response to you local variable and refresh that control via ajax refresh.

Hope this helps. or you can share the sample oml.

I will give you the solution.

Manish Jawla wrote:

Hi Paulo,

what i understood is you are calling one api on the preparation of you page and have some question?

- are you using the response of that api from preparation result? if not why you are calling the api on preparation?

- recommendation- why don't you call the api on search button action and have some local variable to hold the response of your api, so that in the beginning of the page you can show blank placeholder with local variable but on search button you can call api and assign the response to you local variable and refresh that control via ajax refresh.

Hope this helps. or you can share the sample oml.

I will give you the solution.

Hi Manish.

I tried what you said with no results :( 

I'm gonna share the sample oml.

Cheers.

Paulo Solipa wrote:

Manish Jawla wrote:

Hi Paulo,

what i understood is you are calling one api on the preparation of you page and have some question?

- are you using the response of that api from preparation result? if not why you are calling the api on preparation?

- recommendation- why don't you call the api on search button action and have some local variable to hold the response of your api, so that in the beginning of the page you can show blank placeholder with local variable but on search button you can call api and assign the response to you local variable and refresh that control via ajax refresh.

Hope this helps. or you can share the sample oml.

I will give you the solution.

Hi Manish.

I tried what you said with no results :( 

I'm gonna share the sample oml.

Cheers.

Yes, that would really help to resolve the issue asap...Thanks!


Processing Upload...

Hi Paulo,

PFA updated OML, I think it will work. I am not able to test because of missing depedencies but logically I have done the change which you can check.

Ok, first a meta remark, please don't hit the "Reply" button unless you really want to copy the text you are replying to. And if you do, remove the stuff that's not relevant. There are half a dozen one-line replies with like 50 or more lines of original text above it. Doesn't help the clarity!

That said, like Mariano already said, please turn on the logging. This will allow you to see what's going on. The "failed to parse response" errors are very typical for an HTML error page being returned because of faulty input. The logging will show the entire HTTP request and response, which makes a lot clearer what goes wrong.

Kilian Hekhuis wrote:

Ok, first a meta remark, please don't hit the "Reply" button unless you really want to copy the text you are replying to. And if you do, remove the stuff that's not relevant. There are half a dozen one-line replies with like 50 or more lines of original text above it. Doesn't help the clarity!

That said, like Mariano already said, please turn on the logging. This will allow you to see what's going on. The "failed to parse response" errors are very typical for an HTML error page being returned because of faulty input. The logging will show the entire HTTP request and response, which makes a lot clearer what goes wrong.


Hi Kilian.

Thanks for your reply.

Already edit my answers to be more comprehensive. 

And yes the log was already on, that's why I previously referred an error message that appeared:


"Failed to parse response of the method 'GetSkillsearch' of the 'SkillsItsyourskills' REST API:
Parsing 'skills[1].parent_id': Unexpected character encountered while parsing value: [. Path 'skills[1].parent_id', line 1, position 336.
Parsing 'skills[1]': JsonToken EndArray is not valid for closing JsonType Object. Path 'skills[1]', line 1, position 337.
Parsing 'skills[3]': Error converting value "parent_name" to type 'ssHNC_Web.CcSkillsItsyourskills+RESTSkillItem'. Path 'skills[3]', line 1, position 351."


Cheers.

Nikhil Gaur wrote:

Hi Paulo,

PFA updated OML, I think it will work. I am not able to test because of missing depedencies but logically I have done the change which you can check.

Hi Nikhil.

Thank you for your reply.

I'm waiting for the oml for the upload process of your oml.

Cheers.

Hi Paulo,

The error message is from the Error Log. I'm talking about the Integration Log. Please follow the link I posted above.

Kilian Hekhuis wrote:

Hi Paulo,

The error message is from the Error Log. I'm talking about the Integration Log. Please follow the link I posted above.

Hi Julian.

My mistake. Already did that. I did a search for human and this is the error I got, but apparently it's querying.


Well, apparently it returns JSON, but it is not the JSON that OutSystems expects. Which could mean two things:

  1. You haven't defined the output JSON of the REST method properly when consuming the method, or;
  2. The REST service is one of those pesky non-standard conforming REST services that has variable output structures (e.g. sometimes returning an object, sometimes an array of objects).

Could you download the HTTP trace and post the result JSON here?

Kilian Hekhuis wrote:

Well, apparently it returns JSON, but it is not the JSON that OutSystems expects. Which could mean two things:

  1. You haven't defined the output JSON of the REST method properly when consuming the method, or;
  2. The REST service is one of those pesky non-standard conforming REST services that has variable output structures (e.g. sometimes returning an object, sometimes an array of objects).

Could you download the HTTP trace and post the result JSON here?


In attachment goes the Http trace.

Cheers.

Solution

Yeah, it's indeed the latter, as I feared. I pretified the JSON (with this online tool), and this is the first two records returned:

{
   "skills":[
      {
         "id":"7yGaREZnYqEQ8NW4",
         "skill_name":"Human Rights, Laws Related to, Understanding of",
         "parent_id":"NzoyjlabvY6n1rgp",
         "parent_name":"Employment, Laws Related to",
         "synonyms":[

         ],
         "has_sibling":1,
         "has_children":0,
         "has_related_category":0,
         "order_by":"0"
      },
      {
         "id":"KX1wmlo7p43lYVeZ",
         "skill_name":"Human Resources (HR)",
         "parent_id":[

         ],
         "parent_name":[

         ],
         "synonyms":[

         ],

As you can see, "parent_id" and "parent_name" are strings in the first record, but empty arrays(!) in the second one. OutSystems cannot deal with this kind of polymorphous JSON. You can see in the error message that it fails to parse "[", which is the first character of the second "parent_id".

To solve this, you'll need to alter the JSON content in the OnAfterResponse, fixing it so that all records have the same structure. For example, if you can have either a filled "parent_id" or an empty one as array, you could replace the array for an empty string, like this:

Replace(Response.ResponseText, """parent_name""":[]", """parent_name""":""""")

(A single double quote inside a text string should be written as a double double quote, hence all the double quotes.)

And do that for each of the possible offending Attributes.

Solution

Hi Paulo Solipa,

I tried to use the API details which you have shared and for me it is working fine.

Check this:  Search My Skill Sample App


PFA - includes the .oml file


Note: I deleted the parent_id and parent_name attributes from the API method response structure, 

Actually it was throwing Error i.e. "Failed to parse response of the method 'GetSkillsearch' " because of those attribute values.


Hope this helps you!


Regards,

Benjith Sam

Yes, if you don't need those Attributes at all, you can just flat-out remove them, but if you do need them, you need to replace the faulty ones.

Thank you @Killian Hekhuis  and @Benjith Sam . You guys are a lifesaver!!


Cheers,

Paulo Solipa

You're most welcome! tl;dr: always enable the integration logging when trouble shooting, and check the error message thoroughly! :) Happy coding!

You're welcome, Paulo Solipa :)

Happy to help you :)

@Kilian Sir, thank you for the knowledge sharing :)


Regards,

Benjith Sam