Http

API Reference: Net.Http

Http module is used for sending http requests.

For better usage and overcome the quirks of the native handing of http for both platforms, consider using Service-call utility.

RESTful Services

"request()" method of Http provides making a REST request. It takes an argument named method. You can specify the method by setting this parameter.

Handling Response of a Request

Each http method has a "onLoad" callback and a "onError" callback. You can handle error and response using these callbacks.

TypeScript code blocks include examples of how to implement, override and components within the theme. You can create page with the UI Editor to make your page compatible with theming and then you can implement themable components programmatically. Once the page is created with the UI Editor, it generates a class under scripts/generated/pages. You can then extend that class with the following TypeScript classes.

TypeScript
JavaScript
TypeScript
import PageSampleDesign from 'generated/pages/pageSample';
import FlexLayout = require('sf-core/ui/flexlayout');
import Application = require('sf-core/application');
import ImageView = require('sf-core/ui/imageview');
import Http = require("sf-core/net/http");
import Image = require('sf-core/ui/image');
//You should create new Page from UI-Editor and extend with it.
export default class Sample extends PageSampleDesign {
myImageView: ImageView;
myHttp = new Http();
constructor() {
super();
// Overrides super.onShow method
this.onShow = onShow.bind(this, this.onShow.bind(this));
// Overrides super.onLoad method
this.onLoad = onLoad.bind(this, this.onLoad.bind(this));
this.layout.flexDirection = FlexLayout.FlexDirection.ROW;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
}
}
/**
* @event onShow
* This event is called when a page appears on the screen (everytime).
* @param {function} superOnShow super onShow function
* @param {Object} parameters passed from Router.go function
*/
function onShow(superOnShow: () => void) {
const { headerBar } = this;
superOnShow();
Application.statusBar.visible = false;
headerBar.visible = false;
}
/**
* @event onLoad
* This event is called once when page is created.
* @param {function} superOnLoad super onLoad function
*/
function onLoad(superOnLoad: () => void) {
superOnLoad();
this.myHttp.requestImage({
url: 'https://httpbin.org/image/png',
onLoad: (e: {
statusCode: number;
headers: { [key: string]: string };
image: Image;
}): void => {
// Image loaded.
this.myImageView.image = e.image;
},
onError: (e: {
message: string;
body: any;
statusCode: number;
headers: { [key: string]: string };
}): void => {
// Http request image failed.
alert(e.message);
}
});
this.myImageView = new ImageView();
this.layout.addChild(this.myImageView, "myImageView", ".sf-imageView", {
width: 100,
height: 100,
flexProps: {
alignSelf: "CENTER",
},
imageFillType: ImageView.FillType.STRETCH
});
}
JavaScript
const Page = require("sf-core/ui/page");
const extend = require("js-base/core/extend");
const ImageView = require('sf-core/ui/imageview');
const FlexLayout = require('sf-core/ui/flexlayout');
const Http = require("sf-core/net/http");
var PageHttp = extend(Page)(
function PageHttp(_super) {
_super(this, {
onShow: function() {
Application.statusBar.visible = false;
this.headerBar.visible = false;
}
});
var sessionManager = new Http();
var request = sessionManager.requestImage({
url : 'https://httpbin.org/image/png',
onLoad : function(e) {
// Image loaded.
myImageView.image = e.image;
},
onError : function(e) {
// Http request image failed.
alert(e.message);
}
});
var myImageView = new ImageView({
width: 100,
height: 100,
alignSelf: FlexLayout.AlignSelf.CENTER,
imageFillType: ImageView.FillType.STRETCH
});
this.layout.addChild(myImageView);
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
}
);
module.exports = PageHttp;

If you use request method to handle JSON requests, you should parse the result. You should either consider using requestJSON method or service-call utility.

Consider Http Behavior

If the given url is invalid, Http request does not fire onError callback.

Android Permissions

You have to add following permission to your AndroidManifest.xml file before publishing your project.

XML/HTML/SVG
XML/HTML/SVG
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Also you can check Application Permission Management for other permissions.

Notes

If you try to make a request when no network connection, onError callback triggers.

Also, onError callbacks triggers when timeout.

If you pass body parameter with http methods that do not require body parameter, iOS throws an exception, but Android ignores the body parameter and works fine.