ESP_HTTP_CLIENT PUT REQUEST fails every time
Posted: Thu Mar 07, 2019 11:34 am
Hey everyone!
I'm completely lost at this point. I'm trying to do a put request while reading in data from an sdcard. To test it I have written a function as described here: https://docs.espressif.com/projects/esp ... ttp-stream
It fails every time though, I did run the esp_http_client example code and all of the tests passed. I've also tried to run the code I wrote below with various different sites and a local server. It fails each time. I'm quite new to this so not entirely sure what I'm doing wrong. The PUT request below is one of many functions (the rest work), so for now I'm only posting the relevant portion below:
This code returns the following output:
This was actually when I was testing it to my local server (Hence why the header returns logged in!, the prior request was a login to the server afterwhich I passed the cookie in this request), but the output remains the same no matter where I test it. The connection opens and then write fails.
Any help will really be appreciated as I'm completely lost!
TIA!
I'm completely lost at this point. I'm trying to do a put request while reading in data from an sdcard. To test it I have written a function as described here: https://docs.espressif.com/projects/esp ... ttp-stream
It fails every time though, I did run the esp_http_client example code and all of the tests passed. I've also tried to run the code I wrote below with various different sites and a local server. It fails each time. I'm quite new to this so not entirely sure what I'm doing wrong. The PUT request below is one of many functions (the rest work), so for now I'm only posting the relevant portion below:
- void Send_data(char* tstamp,esp_http_client_handle_t client,esp_http_client_config_t config){
- //SEND DATA TO SERVER IN BATCHES
- // SET HEADERS AND HAVE A FIXED CONTENT LENGTH FOR TESTING
- esp_http_client_set_method(client, HTTP_METHOD_PUT);
- esp_http_client_set_header(client, "User-Agent", "python-requests/2.18.4");
- esp_http_client_set_header(client,"Accept-Encoding","gzip, deflate");
- esp_http_client_set_header(client,"Accept","*/*");
- esp_http_client_set_header(client,"Connection","keep-alive");
- esp_http_client_set_header(client,"Content-length","1024");
- //esp_http_client_set_header(client,"Cookie",cookie);
- //SET THE URL FOR TESTING TO HTTPBIN
- esp_http_client_set_url(client, "http://httpbin.org/put");
- //OPEN A CONNECTION AND ENSURE THE LENGTH IS LONGER
- //THAN THE DATA TO BE WRITTEN WITH THE WRITE FUNCTION
- printf(" \n OPEN \n");
- err = esp_http_client_open(client,2000);
- if (err == ESP_OK) {
- ESP_LOGI(TAG, "HTTP OPEN Status = %d, content_length = %d",
- esp_http_client_get_status_code(client),
- esp_http_client_get_content_length(client));
- } else {
- ESP_LOGE(TAG, "HTTP OPEN request failed: %s", esp_err_to_name(err));
- }
- /
- printf(" \n WRITE \n");
- //READ DATA IN FROM SDCARD TO BUFFER
- int ret = 0;
- ret= fread(buffer,1,1024,f);
- printf("\n %d \n",ret);
- //WRITE DATA TO HTTPBIN
- err= esp_http_client_write(client, buff11, ret);
- if (err == ESP_OK) {
- ESP_LOGI(TAG, "HTTP WRITE Status = %d, content_length = %d",
- esp_http_client_get_status_code(client),
- esp_http_client_get_content_length(client));
- } else {
- ESP_LOGE(TAG, "HTTP WRITE request failed: %s", esp_err_to_name(err));
- }
- // FETCH THE RESPONSE HEADERS
- err = esp_http_client_fetch_headers(client);
- if (err == ESP_OK) {
- ESP_LOGI(TAG, "HTTP WRITE Status = %d, content_length = %d",
- esp_http_client_get_status_code(client),
- esp_http_client_get_content_length(client));
- } else {
- ESP_LOGE(TAG, "HTTP WRITE request failed: %s", esp_err_to_name(err));
- }
- printf(" \n CLOSE \n");
- err = esp_http_client_read(client,buff12,1000);
- if (err == ESP_OK) {
- ESP_LOGI(TAG, "HTTP READ_HEADER Status = %d, content_length = %d",
- esp_http_client_get_status_code(client),
- esp_http_client_get_content_length(client));
- } else {
- ESP_LOGE(TAG, "HTTP READ_HEADER request failed: %s", esp_err_to_name(err));
- }
- printf("\n %s \n",buff12);
- //CLOSE THE CONNECTION - CLEANUP ISN'T DONE HERE BUT IN THE NEXT REQUEST
- err = esp_http_client_close(client);
- if (err == ESP_OK) {
- ESP_LOGI(TAG, "HTTP CLOSE Status = %d, content_length = %d",
- esp_http_client_get_status_code(client),
- esp_http_client_get_content_length(client));
- } else {
- ESP_LOGE(TAG, "HTTP CLOSE request failed: %s", esp_err_to_name(err));
- }
- fclose(f);
- free(buff11);
- free(buff12);
- }
This code returns the following output:
- OPEN
- I (7180) HTTP_CLIENT: HTTP OPEN Status = 200, content_length = 38
- WRITE
- 1024E (7180) HTTP_CLIENT: HTTP WRITE request failed: ERROR
- E (12180) HTTP_CLIENT: HTTP WRITE request failed: ESP_FAIL
- CLOSE
- E (17180) HTTP_CLIENT: HTTP READ_HEADER request failed: ERROR
- <h1 style='color:blue'>logged in!</h1>
- I (17180) HTTP_CLIENT: HTTP CLOSE Status = -1, content_length = 38
- EXIT SEND FUNCTION
- I (17180) example: Data sent to database
Any help will really be appreciated as I'm completely lost!
TIA!