Thanks Tim. The book is in the mail...
I'm taking an online course thru Udemy called Modern JavaScript: From the
Beginning...by Brad Traversy.
One of the exercises is creating a library for HTTP requests (which is
included along with my main .js file on the .html page) and that's what
I'm using.
EasyHTTP.js
class EasyHTTP {
// Make an HTTP GET Request
async get(url) {
const response = await fetch(url);
const resData = await response.json();
return resData;
}
// Make an HTTP POST Request
async post(url, data) {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(data)
});
const resData = await response.json();
return resData;
}
// Make an HTTP PUT Request
async put(url, data) {
const response = await fetch(url, {
method: 'PUT',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(data)
});
const resData = await response.json();
return resData;
}
// Make an HTTP DELETE Request
async delete(url) {
const response = await fetch(url, {
method: 'DELETE',
headers: {
'Content-type': 'application/json'
}
});
const resData = await 'Resource deleted...';
return resData;
}
}
Main.js
document.getElementById('start-sweeper').addEventListener('click',
startSweeper);
document.getElementById('stop-sweeper').addEventListener('click',
stopSweeper);
function startSweeper () {
const data = {Id: 'WIEHJ', Action: 'start'};
// Send the request to start the sweeper
http.post('[1]
http://myserver/rest/WIEHJpst/', data)
.then(data => console.log(data))
.catch(err => console.log(err));
}
function stopSweeper() {
const data = {Id: 'WIEHJ', Action: 'stop'}
// Send the request to end the sweeper
http.post('[2]
http://myserver/rest/WIEHJpst/', data)
.then(data => console.log(data))
.catch(err => console.log(err));
}
So I'm using a POST with a promise. Thanks for the advise. I still have
a ways to go to complete the course but I wanted to try out some of what
I've learned so far. And thanks for the mention of the book. I'm sure it
will help clarify some of my other questions.
Thanks,
Rob
On 8/3/2018 4:14 AM, Tim Fathers wrote:
If it's really no more than about 10 seconds then either way is probably
ok, but having the web-service not return until the job has actually
ended is probably the easiest. For longer running back-end tasks then
it's more normal in the web world to return an HTTP response of 202
(Accepted) and get the client to poll periodically to check the status.
Along with a 202 status, the API can also return information about how
the client should request the status and even an estimated completion
time so that you can tell the client "I expect this process to take
about 10 seconds", so that the client can more intelligently poll.
I'm not familiar with what method you're using to create your
server-side code, but it looks like you're using "GET" with an "action"
query parameter like this; GET /myserver/myjob?action=start, which is
quite an old fashioned way of writing a web-service and not really
recommended these days. What you're doing is fine for a bit of hacking
and learning, but you should look at using a more RESTful API if you
want to do it "properly". I only mention this because when I started
out I headed down many blind alleys, learning stuff that I subsequently
found out was out of date, deprecated or just plain wrong.
One final thing, I found the book "RESTFul Webservices Cookbook"
by Sabbu Allamarju really useful at answering the sort of practical
question you asked. Obviously it's more geared towards writing and using
RESTful APIs, and if you're just getting started it might be a little
advanced, but it does help answer many of the questions that arise when
you're coming from the stateful green-screen world to the stateless
web-service world.
Tim.
--------------------------------------------------------------------------
From: WEB400 [3]<web400-bounces@xxxxxxxxxxxx> on behalf of Robert
Rogerson [4]<rogersonra@xxxxxxxxx>
Sent: 02 August 2018 22:08
To: [5]web400@xxxxxxxxxxxx
Subject: [WEB400] Web service advice...
Hi all, first let me state that I'm just learning about web services
using Scott Klement's pdf's.
What I'm trying to accomplish is this. I have a NEP on the V7R1 box
which I'm trying to control from the web.
As a first step I have the html and javascript working to call a web
service to start and stop the job.
What I want to do now is display the status of the job on the page. So
(assuming the job is stopped) when the page is first displayed I would
call my web service and pass action=status which would return stopped
and I would display Inactive on the page.
If I click the start button the web service is called with action=start
and the job is started and the page is updated to Active.
Where I'm looking for advice is when I click the stop button. It may
take 10 seconds for the job to actually end. So between the time that I
call the web service to end the job and when the job is actually ended I
want to display Ending... on the page.
So I'm thinking of two approaches.
Call the web service with action=status in a loop until the status is
ended. Only the update the page to Inactive.
Or when I call the web service to end the job I update the page with
Ending... and do not return a response from the RPG program until the
job is actually ended.
Does anyone have any advise on how to approach this issue.
Thanks,
Rob
--
This is the Web Enabling the IBM i (AS/400 and iSeries) (WEB400) mailing
list
To post a message email: [6]WEB400@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit:
[7]
https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.midrange.com%2Fmailman%2Flistinfo%2Fweb400&data=02%7C01%7C%7C52136dd96ff942f8e44708d5f8b3d1f8%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636688373555483961&sdata=8V6m2FQKKozRc%2BGRGObU8SuEJh56qBC6Rg14ztczkBg%3D&reserved=0
or email: [8]WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at
[9]
https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Farchive.midrange.com%2Fweb400&data=02%7C01%7C%7C52136dd96ff942f8e44708d5f8b3d1f8%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636688373555483961&sdata=l8hRUoqMPLtj0ibpjvCw%2FRc%2BB9CshM69AZOFGJl2unI%3D&reserved=0.
References
Visible links
1.
http://myserver/rest/WIEHJpst/
2.
http://myserver/rest/WIEHJpst/
3. mailto:web400-bounces@xxxxxxxxxxxx
4. mailto:rogersonra@xxxxxxxxx
5. mailto:web400@xxxxxxxxxxxx
6. mailto:WEB400@xxxxxxxxxxxx
7.
https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.midrange.com%2Fmailman%2Flistinfo%2Fweb400&data=02%7C01%7C%7C52136dd96ff942f8e44708d5f8b3d1f8%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636688373555483961&sdata=8V6m2FQKKozRc%2BGRGObU8SuEJh56qBC6Rg14ztczkBg%3D&reserved=0
8. mailto:WEB400-request@xxxxxxxxxxxx
9.
https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Farchive.midrange.com%2Fweb400&data=02%7C01%7C%7C52136dd96ff942f8e44708d5f8b3d1f8%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636688373555483961&sdata=l8hRUoqMPLtj0ibpjvCw%2FRc%2BB9CshM69AZOFGJl2unI%3D&reserved=0
As an Amazon Associate we earn from qualifying purchases.