[Solved] WebClient.get does not work over local network

I am trying to issue HttpClient.get to a local webserver. However, when I do this on my private 192 network, i get a -1 status. If i change to a public network, it works.

assuming that I do not have any network issues (firewall, selinux, port forwarding, etc), I am able to ping my private IP from the Photon without any issue. I can also ping the Photon from my web server without issue.

I am using WebClient version 0.0.5 (the latest).

Running GetHttpJsonLocal.ino (my test application, described below) returns a response.status of -1

This is GetHttpJsonLocal.ino

#include <HttpClient.h>

http_header_t headers[] = 
{
    { "Content-Type", "application/json" },
    { "Accept" , "*/*"},
    { NULL, NULL } 
};

HttpClient http;
http_request_t request;
http_response_t response;

void setup() 
{
    request.ip   = IPAddress(192, 168, 1, 151);
    request.port = 80;
    request.path = "/api/book";
    http.get(request, response, headers);

    Particle.publish("status", response.status);
    Particle.publish("repsonse", response.body);
}

This is GetHttpJsonRemote.ino

#include <HttpClient.h>

http_header_t headers[] = 
{
    { "Content-Type", "application/json" },
    { "Accept" , "*/*"},
    { NULL, NULL } 
};

HttpClient http;
http_request_t request;
http_response_t response;

void setup() 
{
    request.hostname = "jsonplaceholder.typicode.com"; // unfortunatly cant use the ip here
    request.port = 80;
    request.path = "/todos/1";
    http.get(request, response, headers);

    Particle.publish("status", response.status);
    Particle.publish("repsonse", response.body);
}

First, I would verify that your WiFi router allows peer-to-peer networking. Some routers have a setting that limits client communicating to the local network and only allows communicating to the WAN.

Also, where is your loop()?

1 Like

Also as pointed out in the other thread, since you are using default SYSTEM_MODE(AUTOMATIC) the device will try to connect to the Particle cloud and if there is no connection your code won’t run - but with connection your initial WiFi.xxxx() calls are superfluous.
Additionally WiFi.ready() is there to check whether or not the WiFi connection already got established, so there is no use in calling it just like that.

1 Like

You post prompted me to try to reach my local webserver from my phone (also on my local network), which failed. from there I explored where the block was. turned out to be windows firewall, just need to allow port 80 incoming over domain firewall and public firewall.

I guess I assumed that port 80 would be open by default everywhere, i guess not : /

Also, loop() function is not needed as I only need to make the connection once for demonstration purposes.

Thank you for your reply.

Thank you ScruffR. You are correct, I am new to to the Photon (and microcontroller devices in general) and I think having that code there just made me feel better since i had not been able to find the API docs until very recently. Thanks you very much for your replies and guidance. Much Appreciated!

1 Like

I have no Idea how to mark this thread as solved, but for now I’ll just stick a tag in the title

When you click the ellipsis symbol image at the bottom of the post you want to mark as solution, you’ll get a image icon to click.

(I’ve already marked @ninjatill’s post as solution - if you want to mark any other post, you still can)