Giter Site home page Giter Site logo

httpclient's Introduction

HttpClient

HttpClient is a library to make it easier to interact with web servers from Arduino.

Dependencies

  • Requires the new Ethernet library API (with DHCP and DNS) which is in Arduino 1.0 and later

Installation

  1. Download the latest version of the library from https://github.com/amcewen/HttpClient/releases and save the file somewhere
  2. In the Arduino IDE, go to the Sketch -> Import Library -> Add Library... menu option
  3. Find the zip file that you saved in the first step, and choose that
  4. Check that it has been successfully added by opening the Sketch -> Import Library menu. You should now see HttpClient listed among the available libraries.

Usage

In normal usage, handles the outgoing request and Host header. The returned status code is parsed for you, as is the Content-Length header (if present).

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.

See the examples for more detail on how the library is used.

httpclient's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

httpclient's Issues

error when running example

Hello-

When i do a copy/paste of the example with the HttpClient.* files in my project directory, I get these errors:

SimpleHttpExample.cpp: In function ‘void loop()’:
SimpleHttpExample:44: error: ‘HttpClient’ was not declared in this scope
SimpleHttpExample:44: error: expected `;' before ‘http’
SimpleHttpExample:46: error: ‘http’ was not declared in this scope

If I first define http as a global of type HttpClient, then just reference it in the loop() function as http(c), I get:

SimpleHttpExample:14: error: ‘HttpClient’ does not name a type
SimpleHttpExample.cpp: In function ‘void loop()’:
SimpleHttpExample:46: error: ‘http’ was not declared in this scope

Any ideas on how to fix?

thanks,
geremy

Stay connected between read

Hi !
I was wondering if this could be possible to stay connected between read. I'm always reading the same page. This could speed up the request.

Thanks !

Need more descriptive documentation

Do we have another documentation we can refer to? Can't find any description regarding response status codes and how things work. Like, what does -11 mean? Where is the error and how do we even debug it?

Error int b64_encode: control reaches end of non-void function

After I included "HttpClient.h" and "b64.h", at build time it returns the following error:

`/Users/francesco/Documents/Arduino/libraries/HttpClient/b64.cpp: In function 'int b64_encode(const unsigned char*, int, unsigned char*, int)':
/Users/francesco/Documents/Arduino/libraries/HttpClient/b64.cpp:69:1: error: control reaches end of non-void function [-Werror=return-type]
69 | }
| ^
cc1plus: some warnings being treated as errors

exit status 1

Compilation error: exit status 1`

Example with HTTP headers

I tried many times to send headers as instructed but I couldn't be successful. A POST example with headers would enlighten many people.

Warning compiling under PlatformIO

I am using the library compiling under platformIO for the Arduino Uno and I get this warning:

....\HttpClient_ID66\b64.cpp: In function 'int b64_encode(const unsigned char*, int, unsigned char*, int)':
C:\Users\chris.platformio\lib\HttpClient_ID66\b64.cpp:69:1: warning: control reaches end of non-void function [-Wreturn-type]
}

I would be happy to fix it and submit the PR but I am not entirely sure what the function should return. I could see it return some error code or just become a void.

What would you like?

Thanks.

Guru Meditation Error: Core 0 panic'ed (Unhandled debug exception) in POST method

Board | ESP32 nodeMCU |  
IDE name | Arduino IDE |
Flash Frequency | 80Mhz |
PSRAM enabled | no |
Upload Speed | 115200 |  
Computer OS | Windows 10|

I try to send data with post method but I have the next exception:

Guru Meditation Error: Core 1 panic'ed (Unhandled debug exception)
Debug exception reason: Stack canary watchpoint triggered (BeginWIFITask)
Core 1 register dump:
PC : 0x4008c30c PS : 0x00060a36 A0 : 0x40081fac A1 : 0x3ffbab40
A2 : 0x3ffbaff0 A3 : 0x00000003 A4 : 0x00000004 A5 : 0x3ffbafe0
A6 : 0x3ffbac10 A7 : 0x00000014 A8 : 0x00000009 A9 : 0x0000005a
A10 : 0x00000024 A11 : 0x0000002a A12 : 0xffffffff A13 : 0x3f42bff0
A14 : 0x3ffbac10 A15 : 0x3ffbae34 SAR : 0x00000004 EXCCAUSE: 0x00000001
EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffffa

ELF file SHA256: 0000000000000000

Backtrace: 0x4008c30c:0x3ffbab40 0x40081fa9:0x3ffbaf10 0x40114346:0x3ffbafa0 0x400fae0e:0x3ffbafe0 0x400f6ee5:0x3ffbb040 0x400f759b:0x3ffbb0f0 0x400f7727:0x3ffbb150 0x400f774b:0x3ffbb170 0x400d2c02:0x3ffbb190 0x4008966a:0x3ffbb2d0

Rebooting...
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1100
load:0x40078000,len:10900
load:0x40080400,len:6388
entry 0x400806b4

I decoded the exception with ESP32 exception decoder and this is the result:

PC: 0x4008c30c
EXCVADDR: 0x00000000

Decoding stack results
0x40114346: vsnprintf at ../../../.././newlib/libc/stdio/vsnprintf.c line 41
0x400fae0e: log_printf at C:\Users\alvar\Documents\Arduino\hardware\espressif\esp32\cores\esp32\esp32-hal-uart.c line 533
0x400f6ee5: HTTPClient::handleHeaderResponse() at C:\Users\alvar\Documents\Arduino\hardware\espressif\esp32\libraries\HTTPClient\src\HTTPClient.cpp line 1240
0x400f759b: HTTPClient::sendRequest(char const*, unsigned char*, unsigned int) at C:\Users\alvar\Documents\Arduino\hardware\espressif\esp32\libraries\HTTPClient\src\HTTPClient.cpp line 585
0x400f7727: HTTPClient::POST(unsigned char*, unsigned int) at C:\Users\alvar\Documents\Arduino\hardware\espressif\esp32\libraries\HTTPClient\src\HTTPClient.cpp line 493
0x400f774b: HTTPClient::POST(String) at C:\Users\alvar\Documents\Arduino\hardware\espressif\esp32\libraries\HTTPClient\src\HTTPClient.cpp line 498
0x400d2c02: beginWIFITask(void*) at C:\Users\alvar\Documents\Arduino\CO2_Interfaz_WifiH/tasks.ino line 92
0x4008966a: vPortTaskWrapper at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/freertos/port.c line 143

The sketch it's too long because I use a display but this it's the relevant part

void beginWIFITask(void *pvParameters) {


  updateBottomStatus(LV_COLOR_TEAL,"Conectando WIFI: " + ssidName);

  unsigned long startingTime = millis();

  WiFi.begin(ssidName.c_str(), password.c_str());
  while (WiFi.status() != WL_CONNECTED && (millis() - startingTime) < timeout)
  {
    vTaskDelay(250);
  }

   if(WiFi.status() != WL_CONNECTED) {
    updateBottomStatus(LV_COLOR_RED, "Revisa la contraseña y vuelve a intentarlo otra vez");
    vTaskDelay(2500);
    networkScanner();
    vTaskDelete(NULL);
  }

 
  updateBottomStatus(LV_COLOR_GREEN, "CONECTADO CORRECTAMENTE, IP: " +  WiFi.localIP().toString());
 
  while(1){
    if(WiFi.status() == WL_CONNECTED){
     
     delay(100);

     HTTPClient http;

       http.begin(serverName);
        

       // Specify content-type header
       http.addHeader("Content-Type", "application/x-www-form-urlencoded");
       String co2S = String(co2m);
       String tempS = String(temp);
       String humS = String(hum);       
       String httpRequestData = "key=47562jtugj7234785&co2=" + co2S+ "&temp="+ tempS +"&hum=" +humS;
       
       int httpResponseCode = http.POST(httpRequestData);
       
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
      }
      
      http.end();
 
 
      updateBottomStatus(LV_COLOR_BLUE, "::Datos enviados correctamentes a Sequosentry::");


    delay(30000);
    updateBottomStatus(LV_COLOR_GREEN, "::Conectando con Sequosentry::");
    }else{
      
                WiFi.begin(ssidName.c_str(), password.c_str());
                while (WiFi.status() != WL_CONNECTED && (millis() - startingTime) < timeout)
                {
                  vTaskDelay(250);
                }
              
                 if(WiFi.status() != WL_CONNECTED) {
                  updateBottomStatus(LV_COLOR_RED, "Revisa la contraseña y vuelve a intentarlo otra vez");
                  vTaskDelay(2500);
                  networkScanner();
                  vTaskDelete(NULL);
                }
      
    }
  }
    updateBottomStatus(LV_COLOR_RED, "::Se perdio la conexion con la red WIFI::");
    delay(10000);
    networkScanner();
    vTaskDelete(NULL);
}

This it's the line where I have the exception:

0x400d2c02: beginWIFITask(void*) at C:\Users\alvar\Documents\Arduino\CO2_Interfaz_WifiH/tasks.ino line 92

int httpResponseCode = http.POST(httpRequestData);

Thanks

No DELETE?

Why no DELETE? DELETE is a verb just like GET, POST and PUT. In fact, it's a very important part of this complete breakfast.

Do we really need to derive Client class?

Thank you for such a great library. This library helps a lot to make IoT devices very easily.

So I tried to use this library for CC3200 on Energia, but it doesn't work well, because Client class is different from Arduino. Client class of CC3200 needs to implement SSL related pure virtual methods, so HttpClient class instantiation fails. If HttpClient class derives from WiFiClient, it does work.

But I wonder wonder why HttpClient has to derive from Client class? IMHO, Client class is for TCP layer class and HttpClient class is upper layer. So this is not a "is-a" relation, but "has-a" relation, right? Or is there anything rational the class has to inherit?

I've been working on general C++ projects for a while but not much about Arduino projects. If I would miss something, please let me know.

Thanks,

get returns -1

esp32 connected by wifi. first get() after reset returns 200. and following get() returns -1.
Please, Advice

http post request using node mcu

pls anyone help me out to resolve this error
i tried to upload the following code

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "vathsal";
const char* password = "1234567890";

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
WiFi.begin(ssid,password);
}

void loop() {
// put your main code here, to run repeatedly:

if((WiFi.status() == WL_CONNECTED))
{
HTTPClient http;
http.begin("http://wwwkprstaffcom.000webhostapp.com/index.php/iot projecthtml.php");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpcode = http.POST("$_var=helloooo");
http.addHeader("Content-Type", "text/plain"); //Specify content-type header
String payload = http.getString();
Serial.println(httpcode);
Serial.println(payload);
http.end();
}
else{
Serial.println("error in wifi connection");
}
}

AND THEN I FOUND THE FOLLOWING ERRORS

Arduino: 1.8.6 Hourly Build 2018/01/03 03:33 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 4M (1M SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 9600"

Archiving built core (caching) in: C:\Users\Laptop\AppData\Local\Temp\arduino_cache_262920\core\core_esp8266_esp8266_nodemcuv2_CpuFrequency_80,FlashSize_4M1M,LwIPVariant_v2mss536,Debug_Disabled,DebugLevel_None____,UploadSpeed_9600_07078c7af7bb3a9954a4bdc91c11f92d.a
Sketch uses 266887 bytes (25%) of program storage space. Maximum is 1044464 bytes.
Global variables use 34216 bytes (41%) of dynamic memory, leaving 47704 bytes for local variables. Maximum is 81920 bytes.
Uploading 271040 bytes from C:\Users\Laptop\AppData\Local\Temp\arduino_build_47295/basic_iot_3.ino.bin to flash at 0x00000000
................................................................................ [ 30% ]
................................................................................ [ 60% ]
................................................................................ [ 90% ]
.....................An error occurred while uploading the sketch

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Wrong Latitude and longitude.

Success.
latitude=90.0000 longitude=0.0000
Success.
latitude=90.0000 longitude=0.0000
Success.
latitude=90.0000 longitude=0.0000

Missing Content-Length header

I think the write method (at HttpClient.h#L358) should send Content-Length setted to aSide before to finish the request.

question about responseStatusCode

Hi,
I am a newbie of github, so I apologise if I am using this issue tracking to ask a question...

I am using Arduino UNO + Ethernet Shield + xively library (https://github.com/xively/xively_arduino, that it is based upon the HttpClient library) to put some temperature data from a sensor to the xively platform.

Everythig works fine except that happens once in while (sorry... let's say randomly) that Arduino hangs on the xively put function, which recall the HttpClient put + sendHeader + endRequest + responseStatusCode + flush + stop functions.

The question is:
I modified the responseStatusCode adding a timeout condition on the while loop for the informational status code

while ( (iState == eStatusCodeRead) && (iStatusCode < 200) && 
           ( (millis() - timeoutStartResponse) < iHttpResponseTimeout ));

and I don't have freezes anymore, but I am sure (I do not know the http protocoll) that there is a better solution for this problem, maybe it's a server problems, and I would like to know what you think about.

thanks
best regards
Federico

Can't Fetch HTTP 302 Redirect Location

I just use the HTTP_GET, and it will return HTTP 302, then I need fetch the "Location" from Header, I feel so wired it return null from that, please help me here and I will leave issue here! Thanks

Crashes on Galileo when calling readHeader() or skipResponseHeaders()

This library crashes on Galileo when you call skipResponseHeaders() or readHeader(). The issue is that readHeader() is doing this:

if (*iContentLengthPtr == c)

But this pointer gets initialized to 0 instead of kContentLengthPrefix in resetState(), so you are doing a check on a NULL pointer. On a true Arduino device this may not crash, but I bet it doesn't work like it's intended.

The fix is to initialize iContentLengthPtr correctly in resetState():

iContentLengthPtr= kContentLengthPrefix;

Timeouts / Hanging

This has been reported cosm/cosm-arduino#2 and needs verifying.

@sarelvdwalt:

It seems that when hitting the code block: "int ret = cosmclient.put(feed, cosmKey);" there is a hang in the library. For me it occurs at ramdom, but generally if I have network problems or when my network is really busy (on my DSL router). When the hang occurs, it fails to time out, thus hanging up the entire application - a reboot of the Arduino is needed to break the hang.

@errordeveloper:

Firstly we will need to know the exact hardware you are using.

@manosv:

I think its not a "real" hung,it waits a long time for a reply from the server and if that does not happen for some reason the program just halts at that state.It happened to me.I solved by editing the library not to retrieve the code

@errordeveloper:

@manosv, do you think it's more likely to be a bug in amcewen/HttpClient?

@manosv:

Most probably in my opinion.

DHCP hang

Library used with Cosm library, Arduino 1.0.4, board arduino Ethernet.
The board couldn't receive a IP address from DHCP.

I have tried others sketchs with use of standard Ethernet library, and IP address is obtained without problem by DHCP.

Can't retrieve multiple cookies from the response header

** Sorry wrong HttpClient, don't see a way to delete this issue so I will close it**

First, thanks for HttpClient. I'm trying to retrieve multiple cookies from a response header, but I can only retrieve one and it is always the last cookie in the header. I'm using collectHeaders(headerKeys, numberOfHeaders).

Is it possible to retrieve multiple cookies? If not please consider this a feature request.

Private Network Request

Hello !,
I am a newby to Arduino, just wanted to ask how could I send HTTP Request to a local IP address ?
Say a computer on the same Network
Thank you in advance

couldn't find 'client.h'

couldn't find client.h header file used in httpclient.h
Can someone help? thanks in advance.
Manish

not all content is downloaded

I try to download a picture from web server and save to SD.
First what I see is a negative content length.
Content length is: -16328
Way?

The picture content starts with 0x42, 0x4D .... on start index 0.
ID: 0 0x42
ID: 1 0x4D
....
so far so good....

the while loop stops on index 49047 but the full size is 49207
image

Whats wrong with your example code?

here my code

    if (responseCode == 200) {
      responseCode = http.skipResponseHeaders();
      if (responseCode >= 0) {
        int bodyLen = http.contentLength();

        Serial.print("Content length is: ");
        Serial.println(bodyLen);

        unsigned long timeoutStart = millis();
        char c;
        long i = 0;
        // Whilst we haven't timed out & haven't reached the end of the body
        while ( (http.connected() || http.available()) &&
                ((millis() - timeoutStart) < 30000) ) {

          if (http.available()) {
            c = http.read();

            Serial.print("ID: ");
            Serial.print(i);
            Serial.print("\t");
            Serial.print(c);
            Serial.print("\t");
            Serial.println(c, HEX);

            i++;
            myFile.print(c);
            bodyLen--;
            // We read something, reset the timeout counter
            timeoutStart = millis();
          }
          else
          {
            // We haven't got any data, so let's pause to allow some to arrive.
            wait(1000);
          }
        }

        if (!http.connected()) {
          Serial.println("disconnecting.");
          http.stop();
          myFile.close();
          Serial.println("Finished writing to file");
        }
      }
    }

finishRequest()?

I have been trying to make a POST request in JSON format, but with no luck. Server never treats my request as finished and halts until TCP timeout.

Here is what I've learnt by reading the code:

  1. I need to add Content-Type and Content-Length headers
  2. I must call beginRequest() to prevent finishHeaders() in post()
  3. I must call endRequest() before POST body
  4. I should call finishRequest() after POST body

I stuck in step 4 because finishRequest() is mentioned in header but not implemented. According to header comments I simply added iClient->println(); for that.

But the server is still not responding, not even treated it as a finished request.

Any ideas?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.