Setting response of HTTP GET as variable


#1

1) Give a description of the problem
I am trying to hit an API with a GET, and set the response.val as a variable to be displayed, but I’m getting null values.

2) What is the expected behavior?
Have the $response.val be set to a variable after calling it.

3) What is happening/not happening?
Variable remains as null

4) Post a Green Snapshot of the pistonimage
(UPLOAD YOUR IMAGE HERE)
Image: https://ibb.co/h8pULT

5) Attach any logs (From ST IDE and by turning logging level to Full)
(PASTE YOUR LOGS HERE BETWEEN THE MARKS THEN HIGHLIGHT ALL OF THE LOGS SND CLICK ON THE </> ICON TO FORMAT THEM)

REMOVE BELOW AFTER READING
If a solution is found for your question then please mark the post as the solution.


#2

One thing to note is that the request is going to a local http API server.


#3

Try

And change your get request to type json


#4

I changed it to such, and still null.

I changed the request a bit, but I’m still having the same issue :frowning:

For the record here’s the expected return result:

{
“ts”: 1529607760020,
“val”: 12586
}

Do I need to parse the JSON in anyway?


#5

Try removing the .val part of the $response. See if that populates anything to your variable.

Also;
What do you get when you when you put that url from a web browser?

Also;
What are your logs saying?


#6

When I use a cloud based REST API it works, but it doesn’t work on the local calls.


#7

Read somewhere on this forum the other day that pistons are run on a web server, so any local IPs don’t work as the server isn’t in your local network. The solution was to expose the local server to the web and use dynamic dns to keep track of your IP.


#8

This should not be necessary, as long as you are using a local IP address the hub will make the request on your local network.


#9

I don’t disagree it shouldn’t be necessary, I am just saying I am sure I say a similar ask on this forum or the Facebook group and the fix was as I described (I have tried to find it again, but no luck).


#10

Would you please try adding some lines in here to check specifics of the response? Use the Log to console action to check the following:
04%20PM

The last time I worked through debugging on local web requests it turned out to be an authenticated endpoint. The user had entered a password when prompted by the web browser at an earlier time so it was not obvious that authentication was required. I’m not familiar with the Mi Flora in this regard.

Finally, is your hub on the same local 192.168.155.x subnet and able to access any of your other network devices?


#11

@ipaterson the status code and content type are nice and helpful, but how would I go about getting the response body?


#12

never mind… that answer was right in this thread, $response.