Hi,
I am trying to make a simple web service request using the httpclient library library provided by the Web IDE(Particle).
My code is such as:
Serial.println();
Serial.println("Application>\tStart of Loop.");
// Request path and body can be set at runtime or at setup.
request.hostname = "http://www.redit.com/r/funny/new/.json";
request.port = 80;
request.path = "";
// 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() + 3000;
And it returns to me in the console:
Application> Start of Loop.
HttpClient> Connection failed.
Application> Response status: -1
Application> HTTP Response Body:
But, using the ping()
function, i can confirm that the board is really connected to the Internet.
I have already tried with other URLs and the result is the same.
How can I solve this problem?
To start with, try changing this
request.hostname = "www.redit.com"; // only this is the host name
request.port = 80;
request.path = "/r/funny/new/.json"; // and this part is the path
Usually looking at the samples provided with a library is a good starting point to see how things are supposed to be used correctly.
Hi @ScruffR,
Using your example, i got this response in the console:
Application> Start of Loop.
HttpClient> Connecting to: www.redit.com:80
HttpClient> Start of HTTP Request.
GET /r/funny/new/.json HTTP/1.0
Connection: close
HOST: www.redit.com
Accept: */*
HttpClient> End of HTTP Request.
HttpClient> Receiving TCP transaction of 128 bytes.
HTTP/1.1 301 Moved Permanently
Server: Apache
X-Frame-Options: SAMEORIGIN
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Location: http://redit.com/r/funny/new/.json
Vary: Accept-Encoding
X-XSS-Protection: 1; mode=block
Content-Type: text/html; charset=UTF-8
X-Cacheable: YES
Date: Thu, 05 May 2016 17:46:44 GMT
Connection: close
HttpClient> End of TCP transaction.
HttpClient> End of HTTP Response (861ms).
HttpClient> Status Code: 301
Application> Response status: 301
Application> HTTP Response Body:
Testing this URL in the browser, i realize that the server redirects to https connection, so this answer makes sense.
Testing with my server, i got the response status -1 (my server is not https and has all needed headers).
In my application, I will return a json object, so is it possible to receive a json object (I know it will be in plain text) with this library? And is it possible to send a file do the server?
Thanks for help!
If you are not expecting a HTTP response, why not just use TCPClient
instead of HTTPClient
?