I am a beginner in developing SAPUI5 applications and am trying to make a drill down application with a flexible column layout. For my first page I am trying to grab country data from this api (https://www.api-basketball.com/documentation#section/Introduction). I have an API KEY through RapidAPI.
How would I go about grabbing the data and setting it to a model? I was able to print out the response in the console which gave me the data but I'm not sure how I can set this to the model and access the data so I can bind it to my controls in the XML view.
[screengrab of my console with response from the data]: https://i.stack.imgur.com/p1bnq.jpg
onInit: function() {
var myHeaders = new Headers();
// I hid my actual API key with the x's
myHeaders.append("x-rapidapi-key", "XxXxXxXxXxXxXxXxXxXxXxXx");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
//set bball model
var bBallModel = new JSONModel(
fetch("https://api-basketball.p.rapidapi.com/countries", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error)));
this.getView().setModel(bBallModel, "bball")
This is the code in my component.js file. Any help I would really appreciate thank you!
One way would be to update the data after the call succeeds:
onInit: function() {
var myHeaders = new Headers();
// I hid my actual API key with the x's
myHeaders.append("x-rapidapi-key", "XxXxXxXxXxXxXxXxXxXxXxXx");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
var model = new JSONModel();
this.getView().setModel(model, "bball")
//set bball model
fetch("https://api-basketball.p.rapidapi.com/countries", requestOptions)
.then(response => response.text())
.then(result => model.setData(result))
.catch(error => console.log('error', error));
}
Alternatively you can use the loadData method on the JSON model:
onInit: function() {
var uri = "https://api-basketball.p.rapidapi.com/countries";
var model = new JSONModel();
var oHeaders = {
"x-rapidapi-key": "XxXxXxXxXxXxXxXxXxXxXxXx"
};
this.getView().setModel(model, "bball")
model.loadData(uri, null, true, "GET", null, false, oHeaders);
}
Related
I have used a Validation Form to take the input from users and I have a submit button on which submit() function is being called.
insertuserdata() is a function in service.ts file where api is being called. I am passing form data in the api. File(e.g pdf,doc,docx etc.) can also be passed to the api as multi part form data.
But I am using Ionic-Native-Http to pass multi part form data using capacitor.
I am getting the error as status 415.Please help with this.
submit()
{
this.isSubmitted = true;
if (!this.ionicForm.valid) {
console.log('Please provide all the required values!')
return false;
} else {
console.log(this.ionicForm.value)
}
console.log("this is deptid", this.deptid)
console.log("this is description", this.ionicForm.controls["description"].value)
console.log("this is cid", this.ionicForm.controls["subquery"].value)
console.log("this is location", this.ionicForm.controls["location"].value)
console.log("query",this.ionicForm.controls["query"].value)
console.log("Insert Form Data")
const formData = new FormData();
formData.append('postedby', this.employeeId);
formData.append('description', this.ionicForm.controls["description"].value);
formData.append('location', this.ionicForm.controls["location"].value);
formData.append('deptid', this.deptid);
formData.append('cid', this.ionicForm.controls["subquery"].value);
formData.append('file', this.ionicForm.get('profile').value);
console.log("Insert Api call")
this.c2s.insertuserdata(formData).then((insertdata: any) =>{
console.log("Insert Response",insertdata)
})
insertuserdata(formData) {
let httpOptions = {
headers: new HttpHeaders({
'enctype': 'multipart/form-data;',
'Content-Type': 'application/json'
})
};
this.insert = this.http1.post("https://webapplnapp.tatapower.com/tpc_restfull_service/api/connecttosolve/postQuery", formData, httpOptions);
return this.insert
}
I am working on a project with React and a Rails API.
In each of my Axios requests, I want to pass a variable to my API.
Can I configure Axios to tell it to add a variable in the data when I try to POST, DELETE, PUT, PATCH…?
Example:
axios.post('url', { data: 'some_data' }).then(...)
→ API should receive:
data_of_request = { data: 'some_data', added_data_from_config_axios: 'some_variable' }
You can create your own function like this.
const sendPost = (url, data = {}, headers = {}) => {
var body = {...data, added_data_from_config_axios: 'some_variable' };
return axios.post(url, body, { headers });
}
And then, you use this function instead of axios
sendPost(url, { data: 'some_data' }).then(res => {
...
});
Finally I found a better answer.
I just used a built-in function of axios:
const added_data_axios = {
'add_data': '..some_data..'
};
const api = axios.create({
transformRequest: [(data) => {
return {...added_data_axios, ...data};
}, ...axios.defaults.transformRequest],
});
I need to display two numbers in my StandardTile. The data source is a SOAP web service, which I had to call twice with different parameters to obtain these two numbers. Is there any way to fill the tile with these two figures? I tried creating one XMLModel per each ajax call to the web service, and then binding the property of the control to the node from the response, but I'm just getting the same figure duplicated.
Below is my onInit method in the controller
onInit: function () {
// callback from ajax request
SOAPRequester.getMessageOverview(function (data) {
var oModel = new sap.ui.model.xml.XMLModel();
oModel.setData(data);
var oStandardTile = sap.ui.getCore().byId("__xmlview0--messageOverviewTile");
if (oStandardTile !== undefined) {
oStandardTile.setModel(oModel, "overview");
oStandardTile.bindProperty("number", {
path: "/SOAP-ENV:Body/rpl:getMessageListResponse/Response/rn5:number",
model: "overview"
});
}
});
//callback from the second ajax call
SOAPRequester.getErrorMessages(function (callbackData) {
var oModel = new sap.ui.model.xml.XMLModel();
oModel.setData(callbackData);
var oStandardTile = sap.ui.getCore().byId("__xmlview0--messageOverviewTile");
if (oStandardTile !== undefined) {
oStandardTile.setModel(oModel, "overview");
oStandardTile.bindProperty("infoState", "Error");
oStandardTile.bindProperty("info", {
path: "/SOAP-ENV:Body/rpl:getMessageListResponse/Response/rn5:number",
model: "overview"
});
}
});
},
Yes, it is. You are using the same model name twice, thus the firs one is not visible anymore. Simply use different model name, i.e. "overview" and "overview2" or what ever you prefer:
onInit: function () {
// callback from ajax request
SOAPRequester.getMessageOverview(function (data) {
var oModel = new sap.ui.model.xml.XMLModel();
oModel.setData(data);
var oStandardTile = sap.ui.getCore().byId("__xmlview0--messageOverviewTile");
if (oStandardTile !== undefined) {
oStandardTile.setModel(oModel, "overview");
oStandardTile.bindProperty("number", {
path: "/SOAP-ENV:Body/rpl:getMessageListResponse/Response/rn5:number",
model: "overview"
});
}
});
//callback from the second ajax call
SOAPRequester.getErrorMessages(function (callbackData) {
var oModel = new sap.ui.model.xml.XMLModel();
oModel.setData(callbackData);
var oStandardTile = sap.ui.getCore().byId("__xmlview0--messageOverviewTile");
if (oStandardTile !== undefined) {
oStandardTile.setModel(oModel, "overview2");
oStandardTile.bindProperty("infoState", "Error");
oStandardTile.bindProperty("info", {
path: "/SOAP-ENV:Body/rpl:getMessageListResponse/Response/rn5:number",
model: "overview2"
});
}
});
},
Hint: You could also improve your code a little, i.e.
call this.getView().byId("messageOverviewTile") or if you have the right UI5 version this.byId("messageOverviewTile") instead of sap.ui.getCore().byId("__xmlview0--messageOverviewTile")
Do the binding for your controls in your view and then in onInit() call this.getView().setModel(oModel, "overview") and this.getView().setModel(oModel, "overview2")
How to http GET/POST request in ionic2
and what are the data need to import ?
I tried with HTTP GET request in JavaScript? but it does not work for me.
GET Example
this.posts = null;
this.http.get('https://www.reddit.com/r/gifs/top/.json?limit=2&sort=hot').map(res => res.json()).subscribe(data => {
this.posts = data.data.children;
});
console.log(this.posts);
https://www.joshmorony.com/using-http-to-fetch-remote-data-from-a-server-in-ionic-2/
POST Example
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let body = {
message:"do you hear me?"
};
this.http.post('http://spstest.000webhostap..., JSON.stringify(body), {headers: headers})
.map(res => res.json())
.subscribe(data => {
console.log(data);
});
}
https://www.joshmorony.com/how-to-send-data-with-post-requests-in-ionic-2/
Good luck.
For Creating the request firstly we need to add provider by using this command :-
$ ionic g provider restService
here restService is the ts file name in which we write the below code for making request
load() {
console.log(' RestServiceProvider Load Method fro listing');
let postParams = { param1 : '', param2: '' }
if (this.data) {
return Promise.resolve(this.data);
}
// don't have the data yet
return new Promise(resolve => {
this.http.post("YOUR URL", postParams)
.map(res => res.json())
.subscribe(data => {
this.data = data;
resolve(this.data);
});
});
}
In the above code load() is the method of restService class.this method is help out to make the request .This method is called in your other class like this.
this.restSrvProvider.load().then(data => {
let mydata = data;
});
For more knowledge you may go through the ionic blog the
I am using jasmine-protractor e2e framework to test one of our desktop App. I am totally new to this. So if something is not clear please ask.
This is how I am logging in to the server. Server uses SSO for authentication
describe('Protractor', function() {
beforeEach(function() {
browser.ignoreSynchronization = true
browser.get('https://myserver.com/login.html',60000);
});
it('hi', function () {
var btn = element(by.css('.loginFormGroup')).element(by.partialLinkText('Tegile'));
btn.click();
// browser.ignoreSynchronization = false;
var user = element(by.css('.UsernamePasswordTable')).element(By.id('ctl00_ContentPlaceHolder1_UsernameTextBox'));
user.sendKeys('user');
var pass = element(by.css('.UsernamePasswordTable')).element(By.id('ctl00_ContentPlaceHolder1_PasswordTextBox'));
pass.sendKeys('passwd');
var SignIn = element(by.css('.UsernamePasswordTable')).element(By.id('ctl00_ContentPlaceHolder1_SubmitButton'));
// browser.pause();
SignIn.click();
});
After this i would like to execute restapi on the same server. I want it to use same session if possible.
I tried to use request/request, but didnt work. Maybe i was not using it correctly.
You can simply use nodejs http module to make API calls.Look at below examples on how to make both GET and POST calls using http module.
GET call:
var http = require('http');
var headerObj = { Cookie : 'cookie-value' }
var options = {
host: "localhost" ,
path: "/someurl",
port: 8080,
headers : headerObj
};
var req= http.request(options,function(response) {
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
console.log(body);
});
}).on('error', function (err) {
console.log(err);
});
req.end();
POST call:
var http = require('http');
var data = { name : "somename" }; //data that need to be posted.
var options = {
"method": "POST",
"hostname": "localhost",
"port": 8080,
"path": "/someurl",
"headers": {
"content-type": "application/json",
"cache-control": "no-cache",
cookie: 'cookie-value'
}
};
var req = http.request(options, function (res) {
var body = '';
res.on("data", function (chunk) {
body = body + chunk;
});
res.on("end", function () {
console.log(body);
});
});
req.write(JSON.stringify(data));
req.end();
I used SuperAgent to make REST API calls for my application,
below is the link describes the usage of superagent.
npm package superagent