If statement does not recognise false or true

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.

Any ideas?

1 Like

Could it be that any numeric value other than 0 translates to True? So 200 would always execute the ‘do’ part of the If statement.

1 Like

Don’t use “if [response]”. Use “if [status]≠[200]” and put the 200 in a text block.

1 Like

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.

2 Likes

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?

The status ist always 200, no matter if the response is true or false…

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.

Try if response=“true”

1 Like

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:

This did NOT WORK:

but THIS DID WORK:

1 Like

@tmurachpx, you illustrated the issue perfectly!

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.

2 Likes