HttpClient post to influxdb problem

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;
    }

First drop the http:// in the host name.
The protocol denomination is not part of the host name.

Then try again.

2 Likes

Tank you, this was a good step forward!
Now I fight with the problem that I get this error message back:

Response status:400
HTTP Response Body: {“error”:“missing required parameter “q””}

    #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;
        }

THis curl command worked in/outside my network:

curl -G mysite.dlinkddns.com:8086/query --data-urlencode “q=CREATE DATABASE mydb1234”

Is this comment

suggesting that you had tried this and it didn’t work either?
If not, try it.

The code above uses http.get() but get() doesn’t use the request.body; only post() uses request.body.

Since your curl command used get (-G), I think what you want is

request.path = “/query?q%3DCREATE%20DATABASE%20mydb1234";

That’s the URL encoded (—data-urlencode) version appended to the GET query string.

3 Likes

Thank you for this idea but I still get the same error with it:

request.path = "/query?q%3DCREATE%20DATABASE%20mydb1234";

still gives me

Response status:400
HTTP Response Body: {“error”:“missing required parameter “q””}

Just a side note if I enter this into a browser outside my network it works:

mysite.dlinkddns.com:8086/query?q=CREATE DATABASE mydb1234

so I tryed to do the following:

request.path = "/query?q=CREATE DATABASE mydb1234";

But this gives me my first error again:

Response status: -1
HTTP Response Body:

aww solved it with the following line:

request.path = "/query?q=CREATE+DATABASE+mydb1234";

Thank you @rickkas7 and @ScruffR for your help

2 Likes

I am trying to insert data now with the following:

http_header_t headers[] = {
    //{ "Content-Type", "application/json" },
    //  { "Accept" , "application/json" },
    { "Accept" , "*/*"},
    { "User-agent", "Particle HttpClient"},
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

 request.path = "/write?db=mydb1234";
 request.body = "\"cpu_load_short,host=server01,region=us-west value=0.64\"";
 http.post(request, response, headers);

But this gets me this error:

HTTP Response Body: {“error”:“unable to parse
’“cpu_loadshort…value=0.64”’:invalid number”}

Maybe someone has an idea what I have to change to get a post working

Try changing:

request.body = "“cpu_load_short,host=server01,region=us-west value=0.64"”;

to

request.body = “cpu_load_short,host=server01,region=us-west value=0.64”;

because I don’t think you need the embedded double-quotes for the data. I’ve just tried something similar from curl to my Influx database.

oh god that was it, thank you I was sure I allready tryed it without the quotes

It sounds like you are at the same stage of using Influxdb as I was a few weeks ago :smile:

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.

yea thank you I will check it out :wink:
For today I need a break, what I planedto be a 30min tasked developed to a whole day problem :smiley: