Hi I am trying to use the HttpClient lib to post data to my influxdb.
But I alwas get this error message:
Response status: -1
HTTP Response Body:
Here is my Code. I would be really happy if someone can point me to my problem here:
#include "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" },
{ "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 = "http://myurl.dlinkddns.com";
request.port = 8086;
request.path = "/query";
// The library also supports sending a body with your request:
request.body = "{\"q\"=\"CREATE DATABASE mytestdb\"}";
// Post request
http.post(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;
}
#include "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" },
{ "Accept" , "*/*"},
{ "User-agent", "Particle HttpClient"},
{ 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 = "mysite.dlinkddns.com";
request.port = 8086;
request.path = "/query";
// The library also supports sending a body with your request:
request.body = "{\"q\":\"CREATE DATABASE mytestdb2\"}";
//request.body = "q=CREATE DATABASE masdasd";
// 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;
}
It sounds like you are at the same stage of using Influxdb as I was a few weeks ago
I recommend reading https://docs.influxdata.com/influxdb/v0.10/write_protocols/line/ to understand how to set the datatypes of the fields, especially for integers, and escaping spaces etc. You’ll need double-quotes in some places but not in others. It’s not intuitive.