What is the best way to tell if the API is online?
I know I can check if the user has the Internet connection using navigator.onLine but how about the API?
Should I send a request to some simple endpoint and see if the data goes back to me?
I would just simple do a simple "ping" type test, if your API was https://api.example.com/auth/user I would just do a simple GET request on https://api.example.com if this returns the expected result you can assume the API is online. This isn't the best test because there still could be a problem with the API but the concept of it being on line is still checked.
If the service you are using has a status page 9/10 the API will be on there. You could use this page to your advantage by scraping the page and checking the status of the API. Say your using the Bitbucket API you could GET this page status.bitbucket.org and then check that the API state is OPERATIONAL.
Try this under try catch block and log error or do continue rest of the code based on that
try {
//api request
} catch (e)
// log error
// Set flag
}
Based on the flag, do you next action item
Related
I've stumbled upon a problem whilst working on a weather app. I have 401 error popping up every time i'm trying to fetch API from openweathermap.com. I've tried everything so far to fix this problem like waiting some days until my API key would work for me. I also tried to create a new API key and use it but failed at it again. Finally i tried to create another account on openweathermap.com but still have this error. Can anyone help me to find out what is the problem?
P.S. i used a valid API for checking a basic weather info that is available for free subscribtion.
401 Error screenshot
Your API Key is provided as a value to query parameter: appid is wrapped in {}. Remove those braces and perform the request again.
Very often, you will find the usage of {} in the documents, they represent placeholders in a string and are not meant to be part of the final string.
Weathermap Docs: How to make an API Call?
Also, please, make sure that you DO NOT share any essential API Keys in a public forum.
{} should be removed inside the url. It should be
let api = `https://api/openweathermap.org/data/2.5/weather?q=${city}&appid=e2850163218373000f889c28107ac0cf`
I am running into net::ERR_NAME_NOT_RESOLVED when calling the API of my firebase project. I have tried using multiple devices, two internet connections, a VPN, Linux, macOS, Windows 11 to rule out any errors caused by my devices. When navigating to the API link on my browser it does not timeout, and I am provided with a response. The issue seems to be when using the httpsCallable function provided by Firebase. No logs of the function being called are present on firebase outside of navigating to it in a browser.
Here is my code:
const functions = firebase.functions
console.log(functions)
const loginWithCode = httpsCallable(functions, 'loginWithCode')
loginWithCode(loginPayload)
.then((result) => {
console.log(result)
})
.catch((error) => {
console.log("ERROR CAUGHT HERE")
console.log(error)
});
The output from my browser console:
service.ts:206 POST https://us-central1-%22crowd-pleaser-75fd7%22%2C.cloudfunctions.net/loginWithCode net::ERR_NAME_NOT_RESOLVED
App.tsx:79 ERROR CAUGHT HERE
App.tsx:80 FirebaseError: internal
The result from directly inputting the link on the firebase web interface:
{"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}
Is there something I am missing that is creating this issue? I have scoured the internet, and StackOverflow looking for an answer, and all solutions provided have not worked. The method implemented is exactly how it is done on the Firebase docs here
Edit: It seems like the link to which my post request is being sent is formatted oddly. Maybe this could be the issue? I can't figure out why it's formatted this way though.
I found a solution to the problem. My speculation in my edit was correct, the URL to which the post request was being sent by httpsCallable was formatted incorrectly. I am unsure as to why it was being formatted this way, however, the quick solution is to set the customDomain class attribute of the object returned by getFunctions to the correct domain. In my case this was done by doing:
functions.customDomain = functions.customDomain = 'https://us-central1-crowd-pleaser-75fd7.cloudfunctions.net'
The variable 'functions' in the code above is the class attribute returned from the method getFunctions provided by Firebase
The Thing
While I'm not an expert on Firebase the problem is that you're making a wrong HTTP request with loginWithCode(loginPayload), there is nothing wrong with your code that I can see at least.
By the way, you're using:
const loginWithCode = httpsCallable(functions, 'loginWithCode')
rather than a simple const loginWithCode = httpsCallable('addMessage')
as described here: Google FireBase Docs
And then, making a loginWithCode({ text: messageText })
Also, as you can see here: Google Firebase Docs:firebase.functions.HttpsCallable
You will be able to pass any type of data to the HttpsCallable function, so we end at the start point: you're making a wrong HTTP request.
As described in the HTTP answer the error is: net::ERR_NAME_NOT_RESOLVED this happens when a DNS request cannot be resolved, then a domain doesn't exists so this all leads to the thing that there is no way to send the HTTP request since there is not a route in the internet that was found to send it.
The Problem:
While decoding the url that you're making the HTTP request
service.ts:206 POST https://us-central1-%22crowd-pleaser-75fd7%22%2C.cloudfunctions.net/loginWithCode net::ERR_NAME_NOT_RESOLVED
App.tsx:79 ERROR CAUGHT HERE
App.tsx:80 FirebaseError: internal
You will find that you're sending the HTTP request to:
https://us-central1-"crowd-pleaser-75fd7",.cloudfunctions.net/loginWithCode
As you can see, you will find that when making the HTTP request it will be a problem: since you cannot put "crowd-pleaser-75fd7", in the URL to make the HTTP request. That is generating the error net::ERR_NAME_NOT_RESOLVED
I'm not sure what exactly are you trying to do, but I think that the correct URL to the HTTP request should be:
https://us-central1-crowd-pleaser-75fd7.cloudfunctions.net/loginWithCode
With this URL the HTTP request must pass, at least. And I suggest then check the loginPayload in order to fix this.
I'm using social authentication using the vue-google-oauth2 library. It works fine as I am able to authenticate my self and I receive a token from the backend too.
When initially I log in, and by using a function that is part of the vue-google-oauth2 library that I'm using to check if it says that I'm authorized or not, it gives the following response in my browser's console:
this.$gAuth.isAuthorized
true
When I then refresh my browser page, and since I've placed a debugger command in my code, and I print the same function again,
I get the following response:
this.$gAuth.isAuthorized
false
What can I do to ensure that switching tabs, reloading page or refreshing it won't make this happen? Or is this what is actually supposed to be happening?
Have you looked at saving it in as session data? Im not to familiar how Angular state works, but when you set original state you can look for the session key "authorized" and if it doesnt exist set auth to false, if it exists set it to the value.
localstorage.getItem(item)
and
localstorage.setItem(item)
There is also the option of making a component that handles the google auth and sends it to the state.
From the library documentation for vue-google-oauth page you linked it says you need to send that code back to your backend server to create a token to stay signed in, so it's behaving as expected. From here (https://www.npmjs.com/package/vue-google-oauth2#usage---getting-authorization-code) it states :
The authCode that is being returned is the one-time code that you can
send to your backend server, so that the server can exchange for its
own access_token and refresh_token
In other words, you need to do something with that code to make it persist in your app, otherwise it's just a one-time code, so looks to be expected.
I am trying to use the Wordnik API for a project in JS.
When playing around with the API I think I made to many requests and now I get "exceeded access limits" error whenever I make a request.
Is there anything I can do to make the error go away or should I just wait for it to fix itself?
Here are the settings I set (url):
word: apple
limit: 1
partOfSpeech: <none>
includeRelated: false (default)
useCanonical: false (default)
includeTags: false (default)
This is the response I get:
{
"message": "exceeded access limits",
"type": "error"
}
This happens sometimes, just re-enter the api key in the code, and then restart the app, browser or whatever you're testing the api on; and this somehow fixes it
You should check to make sure that you are using your own API key, and not the key that is used for the Wordnik API documentation. :)
If you haven't received your key by email (after signing up for Wordnik and requesting one at http://developer.wordnik.com), check your Wordnik user settings page; log in on Wordnik.com and go to:
https://www.wordnik.com/users/edit
This just started to happen to me today. It was previously working fine for the last 2 weeks. There's no way I've exceeded the limit since we have not released the app yet. I'm thinking it's a bug server side.
I'm using WinJS.xhr to call a ReST service... when I call the url on the browser I can see the complete returned xml. So I want to parse that xml to show some of the data.
WinJS.xhr({ url: "http://myserver/myservice" }).
then(processPosts, downloadError);
The problem is my downloadError function does not have parameters so I have no idea what went wrong.
What am I missing?
The help page is not very helpful :(
Edit: I used to fiddler to see what's on the wire and I don't see the request. The server I'm targeting is my own LAN, I also tried with its IP Address with same results (none)
When there is an error the callback function will take one parameter. The downloadError will need to take in one parameter. If you define downloadError as follows you should get more details. The result type should be XMLHttpRequest and using that you can see the status of the request and why it failed.
function downloadError(result){
//check the result param.
}
EDIT:
Check the app capabilities in your application.AppManifest file. The capabilities section is where you define what capabilities are required by your app for example connect to the internet, use the webcam.