Cannot get/push data to Ubidot using bare-IP with DNSClient (SOLVED)

This topic continues from https://community.spark.io/t/spark-wont-connect-using-tcp-httpclient-libraries-solved/7433

I have a core that won’t resolve hostnames, so I implemented as suggested an external DNS client, which properly resolves to an IP address.

Now I’m revisiting Ubidot’s SparkCore example, below is my code:

#include "HttpClient/HttpClient.h"
#include "application.h"
#include "DNSClient.h"
HttpClient http;
#define VARIABLE_ID "XXX"
#define TOKEN "XXX"
// Mockup variable
int ticker = 0;
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
      { "Content-Type",     "application/json" },
      { "X-Auth-Token" ,    TOKEN },
      { NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
IPAddress dnsServerIP(8,8,8,8);
IPAddress remote_addr;
DNSClient dns;
char serverName[] = "things.ubidots.com";
void postData(void){
    ticker++;
    request.body = '{\"value\":' + String(ticker) + "}";
    http.get(request, response, headers);
}
void setup() {
    // Initialize serial console
    Serial.begin(9600);
    // Initialize server information, get IP via DNS resolv
    request.port = 80;    
    dns.begin(dnsServerIP);
    dns.getHostByName(serverName, remote_addr);
    request.ip = remote_addr;
    request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";
}

void loop() {
    static system_tick_t last = millis();
    if (millis() - last > 10000) {
        postData();
        last = millis();
    }
}

But the nginx server replies with the following:

HttpClient>     Connecting to IP: 50.23.124.68:80
HttpClient>     Start of HTTP Request.
POST /api/v1.6/variables/544fe7627625424c6a6abaf5/values HTTP/1.0
Connection: close
Content-Length: 13
Content-Type: application/json
X-Auth-Token: PuP02Cgumot6RYYOhBAlaSkSnFQO1YodBa8PAglfq5mMQ4g2ewZ8qU0p89uM
196956217026}
HttpClient>     End of HTTP Request.

HttpClient>     Receiving TCP transaction of 128 bytes.
HTTP/1.1 404 Not Found
Server: nginx
Date: Thu, 30 Oct 2014 21:39:49 GMT
Content-Type: text/html
Content-Length: 162
Connection: close
Vary: Accept-Encoding

<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

HttpClient>     End of TCP transaction.

However when testing with the bare-resolved IP in my browser, it displays correctly. Yesterday the server also replied with the same “not found” 404 error code as above, but since my last mail with the Ubidots team, it seems they resolved this already today:

http://50.23.124.68/api/v1.6/variables/544fe7627625424c6a6abaf5/values

HTTP 200 OK
Vary: Accept
Content-Type: text/html; charset=utf-8
Allow: GET, POST, HEAD, OPTIONS

Is there any consideration or step I’m missing? does this also happened to anyone? I could not test my code with a SparkCore without the DNS issue.

Thanks, as always!

–Antonio

Hi @alinanco

Are you sending a “Host: things.ubidots.com” header? That is likely to be required since that hostname resolves to a host that is not unique (its canonical name is 50.23.124.68-static.reverse.softlayer.com) so the server can’t figure out which of the possible web servers that are hosted on this one physical server you might want if you don’t supply a “Host:” header. You can’t send a “Host: 50.23.124.68” header since that is ambiguous.

Crap! Yes, you are right, I missed including the host parameter into the header, now it works as expected.

Thanks @bko as always!

–Antonio

1 Like