I have a program that periodically calls http.get. Generally it works fine and the response.status is 200 but occasionally it returns -1. AFAIK, -1 is not a standard HTTP result code. Can someone tell me what that’s all about? Thanks.
HttpClient http;
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] =
{
// { "Content-Type", "application/json" },
// { "Accept" , "application/json" },
{ "Accept" , "*/*"},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
} ;
http_request_t request;
http_response_t response;
request.hostname = "api.worldweatheronline.com" ;
request.port = 80;
request.path = "/premium/v1/weather.ashx?key=xxxxxxxxxxxxxxxx&q=xxxxx&cc=no&num_of_days=2&tp=12&show_comments=no&format=csv" ;
// The library also supports sending a body with your request:
//request.body = "{\"key\":\"value\"}";
// Get request
http.get(request, response, headers);
@Michele, put #define LOGGING in your code before the #include "HttpClient.h" line and make sure to add Serial.begin(9600); in setup(). This will cause the httpclient library to print out debug message to the Serial (USB) port which you can monitor. The -1 could be cause by various conditions.
Thanks for the suggestion. Unfortunately that’s going to be hard to do since this is my Sparkfun Weathershield and the Photon is out in the back yard where I can’t connect anything to the USB. Would there be a way to write this info to the net, like with some post-to-phant command?
Hi @peekay123, im having the same problem as @Michele. I put the #define LOGGING line in my code but im getting nothing. I know the serial is working because im using it in my own code, so im not sure whats going on. Any ideas?
I have two photons:
One of them is on listening mode and running the “HTTP Soft Ap” web page example from the reference.
The other one is connecting to the first Photon and trying the following, using the httpClient library:
"request.hostname = “http://192.168.0.1”;
request.port = 80;
request.path = “/index.html”;
The httpclient library returns -1 as a error code for a few possible problems. HTTP is carried over TCP, so the library uses the built-in TCPClient to connect to the remote host.
The TCP client could not connect to the remote host. Check your hostname or IP address or DNS server. Or perhaps the port is blocked or wrong.
There was no response body returned from the remote host. This can happen if the remote host does not think you are done sending yet.
I have zero problems with my ip (ping works perfect, wifi signal is strong etc) and I do actually receive a response body (at least my server is sending it and whenever I receive it I see it in my serial monitor).
Can you please suggest me a troubleshooting direction?
The first thing I would try to do is test without the InternetButton library. There could be some kind of interaction there so I would just have your code a GET to each of the URLs in order one every say 10 seconds so I could debug.
In the code you presented, you are using “osmc” as a hostname so I would make sure that exact name resolves in your DNS server. You cannot test this with a web browser since they will automatically try things like “www.osmc” and “oscm.com”. I would make sure using nslookup from another computer and turn on the debug mode to see everything.
Another good thing to try is to use cURL from another computer to simulate the GET requests to your server and make sure they are going OK.
You could also try @peekay123 advice above and turn on logging in the HTTP library but it sounds like you might have tried that already.
Is it possible you could get -1 if the Photon drops its wifi connection? Mine is out in the back yard which is pretty much fringe reception. I’m showing signal strength sometimes as low as -78 dB.
Or better yet, is there a function I can call to tell the current status of the wifi, eg. connected or disconnected? I could get that report via the Bluetooth connection back to the serial LCD.
I, too, seem to be among the folk having trouble with the HttpClient library. I’ve attached mine, and I keep getting Response status: -1. #define LOGGING doesn’t show anything, and I can curl “http://www.cbc.ca/” from my computer successfully.
Have I got #define LOGGING in the correct place? I apparently cannot edit the .cpp and.h files that I’ve added to my project, otherwise I’d put it there…
Thanks for any suggestions! Although I absolutely love Particle.cloud, my goal was to show students how they might access the internet without it. Ironically, the only way at present that we can hit our Heroku web service is by using Particle.publish() and Particle.subscribe().
#define LOGGING
#include "application.h"
#include "HttpClient/HttpClient.h"
/**
* Declaring the variables.
*/
unsigned int nextTime = 0; // Next time to contact the server
HttpClient http;
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
// { "Content-Type", "application/json" },
// { "Accept" , "application/json" },
{"User-Agent","ParticleHttpClient"},
{ "Accept" , "*/*"},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
void setup() {
Serial.begin(9600);
}
void loop() {
if (nextTime > millis()) {
return;
}
Serial.println();
Serial.println("Application>\tStart of Loop.");
// Request path and body can be set at runtime or at setup.
request.hostname = "www.cbc.ca";
request.port = 80;
request.path = "/";
// The library also supports sending a body with your request:
//request.body = "{\"key\":\"value\"}";
// Get request
http.get(request, response, headers);
Serial.print("Application>\tResponse status: ");
Serial.println(response.status);
Serial.print("Application>\tHTTP Response Body: ");
Serial.println(response.body);
nextTime = millis() + 10000;
}
So I just took a quick look and www.cbc.ca (at least where I am) can be served up by an Akamai edge router, so the HOST field in the request is very critical. I see in your debug print that it says HOST: cbc.ca but I would change that to be the exact hostname you are reaching www.cbc.ca instead.
With Akamai hosted content, the physical device you are hitting can host many different web servers at one IP address, so the only way the web server “knows” what content to serve up is the HTTP HOST header.
Thanks, bko! I tried it with www.cbc.ca, and it still didn’t work, but then I tried my university’s website, and it’s working fine. Both www.google.com and www.nytimes.com didn’t work, now I’m beginning to think it’s something funky to do with our network… Either that or something’s amiss with the library. The author himself says it’s a work in progress, is there anything better out there that’s available via the web IDE?
So, just to wrap this up … it has something to do with our university’s firewall. I ran the same code on my home Photon, and, like most things in which a network is involved, worked perfectly. I asked my network admin about this, but I think he’s tired of hearing from me and has deigned to respond
In my case, it was because I was using http:// in front of the URL. Dunno why this should cause the library to choke, seems to me a better implementation would transparently pass that as a known string (as opposed to ftp://, mqtt://, etc…).
I guess you did pass the http:// as part of the hostname name field.
If so, then it's perfectly sensible that an HTTPClient library would choke on that since the protocol identifier is not part of the hostname and as the name of the lib suggests it's meant for HTTP so the protocol identifier doesn't need and shall not be passed.