Learning Web Development (4)

It has been a while since I was here but I decided to continue the series after being busy for a long time.

Part of the solution entails calling a web api from javascript, so I decided to learn how that should be done. Because the TradingAdvice endpoint needs to be there in the future to return the trading advice I decided to get some practice with this.

I took a look at what the gekko ticker stores from an incoming candle, and I found there’s a Unix timestamp in there. That’s an integer. So, even though I probably will not use that integer as a parameter for the api it is very convenient for testing.

Saved candles from the bitcoin exchange I am using

Let’s create an endpoint with an integer as input, outputting a string as result. It can’t get any simpler.

Let’s see if I can call this… And have something returned
This works! In the browser at least.

Next step is to call it from JavaScript, which I am learning…

So I created a web page with a little JS in it, logging to the console first. Let’s see what I get…

Hmm.. CORS? There is a Cross Origin Resource Sharing error here.. So what is wrong? I think it is because (and I am skipping a few steps here) because my request comes from the https website and the api part is http. This is a security thing which is good because the web server protects you from having requests from a different site than yours. In this case it is because the requests are from different port numbers: https is port number 443 and http is port 80. So I need to fix this.

Oh by the way, this is the script I am running which gives the error…

Oops, I was wrong… it was about port numbers but it was because I forgot to put the right port number in the url in the javascript:

Oops…

And of course, fetch expects a json result, not a string, so let’s fix this.

I created a class to represent the tradingadvice..
And now we get the desired result

This has been enough for this evening. I have established that my API is working and can be called from JavaScript.

Next I will be diving more into the Gekko docs and see if I can make it call my API, maybe storing some logs of it anywhere, maybe in the database..

Stay tuned..