Hey Guys,
I’m having an issue with a simple IF check.
Iam using an api for User login and logout.
I want to check weather a usertoken is still valid. The api call is working fine. It gives one of two responses: false or true. Checked it in postman and it works fine. (status 200 and response false or true).
But somehow my blocks always run the stuff inside the if statement. Never the else part. No matter if its true or false.
You are using if [response] which actually means if there is any kind of response then do…
Because [response] is not empty (there is a response) which means True so you will never get the else part.
You can change it to if [response]=“true” which will evaluate to true or false. You need to observe the case of the word “true” to be exactly as the result of your API.
I had that idea too. But I don’t get it to work. The false and true in the response are not transmitted as a string. (see picture below from postman). I tried the if clause with [response = “true”] but that did not work. Do you have any idea on how to extract a boolean value from the response?
Okay, so I re-read your first post and it seems the problem is likely that APIs return JSON which is text. So the “true” and “false” values need to be evaluated as text, not as booleans.
ah yes! That did the trick. Thanks alot. From the Postman response I thought it was not json, but treating it like text did the trick.
Just for documentation if anyone has a similar problem:
The web API is returning a TEXT value of “false” or “true”. That is different from the little green blocks of true and false (JavaScript Booleans). it can be helpful to remember that the blocks in Thunkable are wrapper for Javascript. When the blocks behave in unexpected ways, look up how the Javascript equivalent behaves and it will ususally point you in the direction to correct (or work around) the issue.
As a rule (and I have encountered exceptions), most Web APIs return a text string. Sometimes it is raw text, other times it is formatted JSON.