2

Apologies if this question appears to be too vague but I would like to know the key differences between these two clients.

While WiFiClient comes pre-loaded with Arduino IDE, HttpClient doesn't. Also, to send HTTP commands to a server (XML/JSON), which one to go for?

4

WiFiClient provides a raw connection to the internet through a WiFi access point (such as your home router).

HttpClient provides a connection to a web server via an existing Client connection, such as a WiFiClient connection.

So the simplest answer to your question

to send HTTP commands to a server (XML/JSON), which one to go for?

is "Use both".

You use WiFiClient to connect to the internet, then you use HttpClient on your WiFiClient connection to communicate with a website. As it states in the readme on HttpClient's github repository:

Because it expects an object of type Client, you can use it with any of the networking classes that derive from that. Which means it will work with EthernetClient, WiFiClient and GSMClient.

To understand the concept more fully you will first need to understand the OSI 7-Layer Model which is essential reading for anyone doing anything with networking software.

| improve this answer | |
  • Can you tell me why aren't they using HttpClient on top of EthernetClient over here: gist.githubusercontent.com/jamesabruce/8bc9faf3c06e30c3f6fc/raw/… – Hyperbola Mar 1 '16 at 11:14
  • 1
    @Hyperbola Because you don't have to use HttpClient. You can do it all yourself manually if you like. HttpClient just saves you the hassle is all. – Majenko Mar 1 '16 at 11:15
  • Thanks @Majenko. I have a question posted too regarding Philips Hue; if you could look at it once. – Hyperbola Mar 1 '16 at 11:18
  • 1
    @Hyperbola I have no clue what a Philips Hue is nor how you communicate with it. I would recommend starting simpler and trying to send a GET request to somewhere like Google before dabbling into unknown boxes of tricks. – Majenko Mar 1 '16 at 11:21
1

As Majenko Said,

WiFiClient is raw in the sense you have to deal with the HTTP protocol stuff yourself, a shot snippet of raw GET request is provided below

  WiFiClient wc;
  wc.print("GET ");
  wc.print(/xml/device_description.xml);
  wc.println(" HTTP/1.1");
  wc.print("Host: ");
  wc.println(selectedDevice->getIp());
  wc.println("Connection: close");  //automatically close Connection
  wc.println("");

each line is terminated with "\r\n", the request is terminated with an empty line

Where as the HttpClient is a library which handle all the low level HTTP protocol stuff and provide simple interface to sent HTTP GET/POST request to any device that support HTTP

HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "text/plain");
int httpResponseCode = http.POST("Hi Sending POST request");
Serial.println(httpResponseCode);
http.end();

To send JSON change the header with the below line

http.addHeader("Content-Type", "application/json");

So the answer is if you want to deal with the low level HTTP protocol use WiFiClient else use HTTPClient

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.