Prblem using HTTP REST API

Hello all,
I’m trying to read/write pin value from a web page.
I’m following the example in the api documentation:
https://blynkapi.docs.apiary.io/#reference/0/write-pin-value-via-get/write-pin-value-via-get

If I enter the url directly in the browser I can succesfully write the pin.
If I try to do it via javascript as written in the api documentation it doesn’t work.
The strange thing is also that I get “0” as returned stats value.

Here below the simple example that I’m trying to use (of course auth_token as to be replaced with your token).

Could anybody help me?

Thank you

<!DOCTYPE html>
<html>
<body>

<h2>The XMLHttpRequest Object</h2>

<button type="button" onclick="WritePin()">Change Content</button>
<p> Status: </p>
<p id="Status"> </p>

<script>
function WritePin() {
var request = new XMLHttpRequest();

request.open('GET', 'http://blynk-cloud.com/auth_token/update/V0?value=0');

request.onreadystatechange = function () {
  if (this.readyState === 4) {
    console.log('Status:', this.status);
    console.log('Headers:', this.getAllResponseHeaders());
    console.log('Body:', this.responseText);
    document.getElementById("Status").innerHTML = this.status;
  }
};

request.send();
}
</script>

</body>
</html>

If you’re running an API call on a remote server then its always a good idea to put the actual IP address of the Blynk cloud server that holds your project, rather than allowing the remote server to try to resolve the IP address itself, as it could hit the wrong server.
Ping blynk-cloud.com from your home PC and use that IP address in your API call.

Pete.

Hello PeteKnight,
thank you for your reply.
I have tried with http://139.59.206.133/auth_token/update/V0?value=0 but I have the same result.
If I use it from the url bar in the browser it works, if I use it inside JavaScript it doesn’t work and it returns 0.
Any other idea?

I know nothing about HTML or the function that you are using to make this API call, but the API call needs to be a JSON request. Is that what this does?

Pete.

Hello, I’m not an expert of html also, but I think that with http GET method it is not needed the JSON.
Anyway my example is taken directly from the blynk rest api documentation, you can see that here:
https://blynkapi.docs.apiary.io/#reference/0/write-pin-value-via-get/write-pin-value-via-get

I cannot understand how can work the example in the api documentation. Probably I’m missing something, but I don’t know.

Try to use like that.
uses resquest the “body” instead “status”

it works for me

<body>

<h2>The XMLHttpRequest Object</h2>

<button type="button" onclick="WritePin()">Change Content</button>
<p> Status: </p>
<p id="Body"> </p>


<script>
function WritePin() {
var request = new XMLHttpRequest();

request.open('GET', 'http://blynk-cloud.com/AUTHTOKEN/update/V0?value=TEST');

request.onreadystatechange = function () {
  if (this.readyState === 4) {
    console.log('Status:', this.status);
    console.log('Headers:', this.getAllResponseHeaders());
    console.log('Body:', this.responseText);
    document.getElementById("Body").innerHTML = this.responseText;
  }
};

request.send();
}
</script>

</body>
</html>

Hello @juninhuhlima,
thank you for your code.
Actually the http get request in our codes are the same, log the body instead the status will change only the effect of the returned value displayed, but not the http request.
Due to you are telling me that for you is working, I’ve thinked what could be wrong in my setup and finally I found the issue: I was trying the code using the HTML editor of https://www.w3schools.com/html/tryit and with this the code doesn’t work.
I have just edited manually a text file with the same code, changed the extension in html and opened it with the browser and it works!
So the code was correctly from the begging.
I have also verified that the correct return value is in status and not in body.

Thank you all for the support

1 Like