Hi, i’m triyng to send an http request that contatins two headers: one for the username and another for the password. I have two questions: is the code i wrote correct? And the other: how can i access to the console to see serial prints? Is there a more simple way to display the response?
Here’s the code:
#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" },
{ "username" , "myuser"},
{"password" , " mypass"},
{ 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 = " myhost";
request.port = 80;
request.path = "mypath";
// 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;
}
To answer the first question, we'd like to know what OS you're using and what IDE, since that inflluences the answer.
And once that's established, the answer for the second part would be: "That's pretty simple and won't get a lot simpler otherwise"
To see console output you will need a serial terminal. ParticleDev contains one. Arduino ide too. I usually use ParticleDev one or Termite.
I’m using windows. I opened the serial port on the arduino ide and i got this messages:
Application> Start of Loop.
Application> Response status: -1
Application> HTTP Response Body:
with nothing after response body
The response status of -1 indicates that there was a problem with the request, so no surprise that the body is empty.
One possible reason might be - since you redacted the actualy value - a wrong hostname.
I’m using this as hostname https://ghost.overtechnologies.com/rest/plant/remote_access/port/,
and nothing into path. Am i missing something?
That’s not a hostname.
The hostname for that would be “ghost.overtechnologies.com”.
Everything after that is the path
And since this is a https:// URL you won’t get happy with HTTPClient
since that doesn’t support secure HTTPS.
So the lesson here, over-simplifying/obfuscating the wrong things makes getting a good answer quick rather difficult.
So is there a way to make it work with https?
You can have a forum search.
This is a widely discussed matter - but there is no native support for HTTPS yet.
The best way with pure Particle tools is using webhooks.
I’m using webhooks, i set the url to https://ghost.overtechnologies.com/rest/plant/remote_access/port/
and two headers for password and username. When i test it i receive the message connectipn times out…