Http Calls
  • 25 Jan 2024
  • 1 Minute to read
  • Contributors
  • PDF

Http Calls

  • PDF

Article summary

Http Calls

Http calls are performed with the help of classes provided by Data Flow.

Example

A sample call to accuweather that gets the air temperature of İstanbul and uses this information to send an event would be as follows:

var request = new SimpleHttpRequest();	
request.url = "https://dataservice.accuweather.com/currentconditions/v1/318251?apikey=write_your_apikey_here";
var credentialId = "";	
 new HttpAgent().call(
     request, 
     credentialId,
     function(response){
		if (200 != response.httpStatusCode)
		{
            return;
        }
        nodeBase.log(LogLevel.info, "weather response : " + JSON.stringify(response));
        var weatherJson = JSON.parse(response.body);
        var degrees = weatherJson[0].Temperature.Metric.Value;
        nodeBase.log(LogLevel.info, degrees + " C degrees");

        var outData = { "degrees": degrees, "city": cityName };
        var eventInformation = new EventInformation(
            channelIndex, 
            "accuweather", 
            "weather", 
            outData,
            eventInformation.StartSampleIndex, 
            eventInformation.EndSampleIndex
        )
        raiseEvent(eventInformation);
     }
);

SimpleHttpRequest

SimpleHttpRequest is the http request object we use with HttpAgent.

Properties

namedefaultdefinition
typeHttpRequestType.getHttpRequestType.get or HttpRequestType.post
urlemptyurl as a string
headersan empty Objecta string dictionary
bodyemptybody content as a string
ignoreSslErrorsfalseWhen set to true any https certificate errors will be ignored

HttpAgent

HttpAgent is the class provided by Data Flow for custom nodes' http client needs.

call

HttpAgent has the method call that performs an async http request.

call(simpleHttpRequest, credentialId, responseListener)

Parameters:

nameexplanation
simpleHttpRequestthe request object.
credentialIdId of a credential that will be used in this request. May be empty for requests that does not require authentication. The Data Flow credentials are managed in Credential page
responseListenerA function object that will be called when the http request has completed. function(response){} It has a response object of type SimpleHttpResponse as its only parameter.

SimpleHttpResponse

SimpleHttpResponse is the http response object we use with HttpAgent.

Properties

namedefinition
headersresponse headers
bodyresponse body
networkErrorCode0 for success. Error code from the underlying operating system or network stack.
httpStatusCodeHttp status code for this response.
httpStatusTextTextual explanation for http errors

Was this article helpful?

What's Next
Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.