How to use a REST API to do a POST with Spark Core?

Hi! I’m trying to do a Spark Core code to use the REST API from theThings.IO. I want to Post the value 0. I did this code and it compiles but it doesn’t work. Does anybody know what the problem is? I failed trying to communicate with Xively too. Help please!

#include "HttpClient/HttpClient.h"
 
HttpClient http;
#define THINGTOKEN "XXXXXXXX"
#define USERTOKEN "theThingsIO-Token: XXXXXXXXX"
 
 
http_header_t headers[] = {
                { "Accept", "application/json" },
                { "Authorization" , USERTOKEN },
                { "Content-Type", "application/json" },
                { NULL, NULL }
};
http_request_t request;
http_response_t response;
 
void setup() {
                request.hostname = "api.thethings.io";
                request.port = 80;
                Serial.begin(9600);
}
 
void loop() {
                request.path = "/v1/ThingWrite";
                String jsonString ="{";
                jsonString +="\"thing\":{";
                jsonString +="\"id\":";
                jsonString +=THINGTOKEN;
                jsonString +="},";
                jsonString +="\"values\":";
                jsonString +="[";
                jsonString +="{";
                jsonString +="\"key\":\"air\",";
                jsonString +="\"value\":";
                jsonString +="0";
                jsonString +="";
                jsonString +="\"units\":\"lumens\",";
                jsonString +="\"type\":\"temporal\"";
                jsonString +="}";
                jsonString +="]";
                jsonString +="}";
                request.body =jsonString;
                http.post(request, response, headers);
                Serial.println(response.status);
                Serial.println(response.body);
                delay(1000);
}

Thanks @peekay123 for doing this :+1:

Can you provide the output from HTTPClient. Normaly this library prints informations about whats happened.
Please also try to use IP and include the “Hostname” Header to ensure that the api server is called correctly

request.hostname = null;
request. ip = {77,73,82,243};

and add

{ "Host" , "api.thethings.io"},

to your header

Sorry but I don’t know how to see the serial commands. I don’t see any serial monitor in the Spark Core IDE. I tried to use Putty (Windows) but I didn’t succeed because the program tells me that the COM port is already in use. What is this problem? Thank you very much.