Need to call HTTP REST API with JSON request from FD Serverless app

Hi Team,

We are trying to call a HTTP REST API from a FD severless app. Please let us know how this can be achieved? We need to call REST API via:

  1. JSON Request
  2. QueryString URL

Hi Rohan,

You can definitely do this. You can make use of our $request feature to make REST API calls and the url can be one with query strings as well. You can go through the documentation for Request API to know more about this. A sample request using querystring URL would be something like -

 $request.get("https://www.example.com/api/contacts?type=2&age>23", options)
    .then(
        function(data) {
               //handle "data"
               //"data" is a json string with status, headers, and response.
        },
        function(error) {
              //handle failure
        }
 );
1 Like

Hi Rohith,

Thanks for the information. Actually our requirement involves sending JSON Request body and making POST request instead of GET. Can you please provide a sample for that as well? We’ll go through the documentation as well, but it would be great if you can provide a sample of JSON request body with POST request.

Hi Rohith/Team,

Could you please help with the query posted above? Thanks

Hi @rohan15,

Good day ! We have a few helpful samples in the sample app repository as well. I would recommend you to take a look at it :

  • Using client.request.post

Also, this post would be helpful if you are going to make use of iparams along the way

Hi Hemchander, Team

Thanks for providing the example. We had created a serverless app and tried to call a public REST API. The below code is written in server.js, but we are getting an error stating “client” is not defined. Can you please suggest, where it has to be defined to solve this error?

Server.js

exports = {

events: [
{ event: ‘onTicketCreate’, callback: ‘onTicketCreateHandler’ }
],

// args is a JSON block containing the payload information.
// args[‘iparam’] will contain the installation parameter values.
onTicketCreateHandler: function(args) {
console.log('Hello ’ + args[‘data’][‘requester’][‘name’]);

  var options = {
	headers: {
		
	},
	body: JSON.stringify({
		"body": { "email": "eve.holt@reqres.in", "password": "cityslicka"}
	})
};
  
  client.request.post(`https://reqres.in/api/login`, options)
	.then(function (data) {
		// TODO : Add try catch block
        console.log("response received "+data);
		response = JSON.parse(data.response);
		
	})
	.catch(function (error) {
		console.error("error", error);
	})

}

};

Error:
server.js:230
client.request.post(https://reqres.in/api/login, options).then(function (d
ata) {
^

ReferenceError: client is not defined
at Object.onTicketCreateHandler (server.js:230:5)
at server.js:1:68
at server.js:1:1546
at Script.runInContext (vm.js:133:20)
at ProductEvent.sandboxExecutor (C:\new\nvm\v10.18.0\node_modules\fdk\lib\ev
ent_handler\framework.js:230:25)
at ProductEvent.execute (C:\new\nvm\v10.18.0\node_modules\fdk\lib\event_hand
ler\framework.js:224:10)
at ProductEvent.execute (C:\new\nvm\v10.18.0\node_modules\fdk\lib\event_hand
ler\framework.js:305:13)
at handler (C:\new\nvm\v10.18.0\node_modules\fdk\lib\event_handler\framework
.js:349:11)
at handleRequest (C:\new\nvm\v10.18.0\node_modules\fdk\lib\routes\beevents.j
s:83:3)
at Layer.handle [as handle_request] (C:\new\nvm\v10.18.0\node_modules\fdk\no
de_modules\express\lib\router\layer.js:95:5)

Hi Team,

Incase any other configuration is required in the above, please let us know that as well. Actually, we are developing a FD app for the first time, and have written the above code in server.js to call a dummy public URL which returns json response.

Hi @rohan15, If it is a serverless app, please make use of $request instead of client.request in server.js - Refer here - Request API for further details

Hi Hemchander S/Team, Thank you for the details. We were able to call the API with $request.
As per our requirement, after calling 1st api with JSON request, we are supposed to do the following in the same serverless app (server.js file) only:

  1. Make a call to a 2nd API URL with GET request. (This we will be achieve with the documentation) .
  2. Now the 2nd API (mentioned in point 1), does not return JSON response. It does a URL redirection to our application(we can let them know to which URL to redirect) and appends a “AUTHORIZATION CODE”, in the url like this: ?code=&scope=

So our Main Requirement is to get the Authorize Code from the callback URL.This has to be done in server.js only which is invoked on TicketCreation.

Can you please suggest how we can achieve this? Would be really helpful if you could provide a sample like before.

Hi Team,

In continuation of the above question, if we are to provide a redirection URL of our app to the API which we are calling, what would be the URL of our serverless app? It is understood that it is server.js is invoked during onTicketCreation, so does this type of app have any URL of its own, which we can use for this redirection purpose and read the TOKEN from text appended to the URL?

Hi Team,

Please help with the queries posted below:
We were able to call the API with $request.
As per our requirement, after calling 1st api with JSON request, we are supposed to do the following in the same serverless app (server.js file) only:

  1. Make a call to a 2nd API URL with GET request. (This we will be achieve with the documentation) .
  2. Now the 2nd API (mentioned in point 1), does not return JSON response. It does a URL redirection to our application(we can let them know to which URL to redirect) and appends a “AUTHORIZATION CODE”, in the url like this: ?code=&scope=

So our Main Requirement is to get the Authorize Code from the callback URL.This has to be done in server.js only which is invoked on TicketCreation.

Can you please suggest how we can achieve this? Would be really helpful if you could provide a sample like before.

In continuation of the above question, if we are to provide a redirection URL of our app to the API which we are calling, what would be the URL of our serverless app? It is understood that it is server.js is invoked during onTicketCreation, so does this type of app have any URL of its own, which we can use for this redirection purpose and read the TOKEN from text appended to the URL?

Hi Hemchander, Team

Can you please provide your suggestions on how we can achieve the objective ,mentioned in the above post? Thanks.

From the flow that you have described, I infer that you are trying to perform OAuth? Have you considered the platform’s OAuth feature already ? Refer -> OAuth

Also, can you elaborate as to which third-party API are you trying to authenticate against ? Since you are already using serverless apps, there is a very high chance that you can make use of certain NPM libraries that can take care of this

Actually the API which we are calling can redirect to any URL which is specified by us. Along with the redirection it will append an authorization code to the URL. So our basic objective is to provide a URL to the third party to which they can redirect, and from which our FD Serverless App can Pick up the authorization code.

So when I check the OAuth link, I am not able to get how we pick this code from the redirected url…Also, which URL can we provide? Our serverless app is invoked on ticket creation event, it doesn’t have a URL of its own right?

Hello @rohan15,

I see you have created another topic here.

Thank you for creating another topic. Let use continue this discussion in the new thread.

This topic was automatically closed after 2 days. New replies are no longer allowed.