First, thank you nmattlsson for creating the library and ryotsuke for your work adding the IP address and useful posts.
I am trying to use the library in the web IDE (only been playing with the spark for a day so I have not had time to try the netbeans thing). I think i am close to getting it work but it is not compiling.
I added the application.h, HttpClient.cpp and HttpClient.h as additional files in the web IDE.
One thing i noticed is that the http.post method seems to have the order of request and response members reversed from the get method. is this correct?
Does anyone have a working POST example?
Thanks in advance:
Here is my application code:
#include "application.h"
#include "HttpClient.h"
#include <string.h>
/**
* Declaring the variables.
*/
unsigned int nextTime = 0; // Next time to contact the server
HttpClient http;
const char method[] = "POST";
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
//{ "Content-Type", "application/json" },
// { "Accept" , "application/json" },
{ "Accept" , "*/*"},
{ 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 = "www.timeapi.org";
request.ip = {192,168,1,12};
request.port = 9000;
request.path = "/post_beacon";
// The library also supports sending a body with your request:
request.body = "{\"accuracy\":1.4611688320646095,\"clientId\":\"317a1f90-a4c0-11e3-88e2-9d56db0bd2c7\",\"id\":\"aa50a994-b062-4d4d-8b5c-188e61343b2e\",\"majorId\":2,\"minorId\":4,\"power\":-74,\"proximity\":2,\"proximityId\":\"2f234454-cf6d-4a0f-adf2-f4911ba9ffa6\",\"remoteTime\":1402723272866,\"rssi\":-78,\"userId\":\"24b0fc20-a3df-11e3-996e-d50f69be8660\"},{\"accuracy\":1.01076,\"clientId\":\"317a1f90-a4c0-11e3-88e2-9d56db0bd2c7\",\"id\":\"abee4413-f276-4e1b-94f6-c93ce95cd2b6\",\"majorId\":2,\"minorId\":7,\"power\":-76,\"proximity\":2,\"proximityId\":\"2f234454-cf6d-4a0f-adf2-f4911ba9ffa6\",\"remoteTime\":1402723272866,\"rssi\":-76,\"userId\":\"24b0fc20-a3df-11e3-996e-d50f69be8660\"}";
// Get request
http.post(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;
}
And here is the error trace when i compile:
In file included from ../inc/spark_wiring.h:30:0,
from ../inc/spark_wiring_stream.h:36,
from ../inc/spark_wiring_client.h:24,
from ../inc/spark_wiring_tcpclient.h:29,
from /HttpClient.h:5,
from /HttpClient.cpp:1:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
In file included from ../../core-common-lib/CC3000_Host_Driver/evnt_handler.h:38:0,
from ../inc/spark_wlan.h:33,
from ../inc/main.h:38,
from ../inc/spark_utilities.h:30,
from ../inc/spark_wiring.h:34,
from ../inc/spark_wiring_stream.h:36,
from ../inc/spark_wiring_client.h:24,
from ../inc/spark_wiring_tcpclient.h:29,
from /HttpClient.h:5,
from /HttpClient.cpp:1:
../../core-common-lib/CC3000_Host_Driver/socket.h:146:0: warning: "fd_set" redefined [enabled by default]
#define fd_set _types_fd_set_cc3000
^
In file included from /opt/gcc_arm/arm-none-eabi/include/stdio.h:47:0,
from ../inc/spark_wiring_print.h:30,
from ../inc/spark_wiring_client.h:23,
from ../inc/spark_wiring_tcpclient.h:29,
from /HttpClient.h:5,
from /HttpClient.cpp:1:
/opt/gcc_arm/arm-none-eabi/include/sys/types.h:256:0: note: this is the location of the previous definition
#define fd_set _types_fd_set
^
In file included from ../../core-common-lib/CC3000_Host_Driver/evnt_handler.h:38:0,
from ../inc/spark_wlan.h:33,
from ../inc/main.h:38,
from ../inc/spark_utilities.h:30,
from ../inc/spark_wiring.h:34,
from ../inc/spark_wiring_stream.h:36,
from ../inc/spark_wiring_client.h:24,
from ../inc/spark_wiring_tcpclient.h:29,
from /HttpClient.h:5,
from /HttpClient.cpp:1:
../../core-common-lib/CC3000_Host_Driver/socket.h:162:0: warning: "FD_SET" redefined [enabled by default]
#define FD_SET(fd, fdsetp) __FD_SET (fd, fdsetp)
^
In file included from /opt/gcc_arm/arm-none-eabi/include/stdio.h:47:0,
from ../inc/spark_wiring_print.h:30,
from ../inc/spark_wiring_client.h:23,
from ../inc/spark_wiring_tcpclient.h:29,
from /HttpClient.h:5,
from /HttpClient.cpp:1:
/opt/gcc_arm/arm-none-eabi/include/sys/types.h:258:0: note: this is the location of the previous definition
# define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1L << ((n) % NFDBITS)))
^
In file included from ../../core-common-lib/CC3000_Host_Driver/evnt_handler.h:38:0,
from ../inc/spark_wlan.h:33,
from ../inc/main.h:38,
from ../inc/spark_utilities.h:30,
from ../inc/spark_wiring.h:34,
from ../inc/spark_wiring_stream.h:36,
from ../inc/spark_wiring_client.h:24,
from ../inc/spark_wiring_tcpclient.h:29,
from /HttpClient.h:5,
from /HttpClient.cpp:1:
../../core-common-lib/CC3000_Host_Driver/socket.h:163:0: warning: "FD_CLR" redefined [enabled by default]
#define FD_CLR(fd, fdsetp) __FD_CLR (fd, fdsetp)
^
In file included from /opt/gcc_arm/arm-none-eabi/include/stdio.h:47:0,
from ../inc/spark_wiring_print.h:30,
from ../inc/spark_wiring_client.h:23,
from ../inc/spark_wiring_tcpclient.h:29,
from /HttpClient.h:5,
from /HttpClient.cpp:1:
/opt/gcc_arm/arm-none-eabi/include/sys/types.h:259:0: note: this is the location of the previous definition
# define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1L << ((n) % NFDBITS)))
^
In file included from ../../core-common-lib/CC3000_Host_Driver/evnt_handler.h:38:0,
from ../inc/spark_wlan.h:33,
from ../inc/main.h:38,
from ../inc/spark_utilities.h:30,
from ../inc/spark_wiring.h:34,
from ../inc/spark_wiring_stream.h:36,
from ../inc/spark_wiring_client.h:24,
from ../inc/spark_wiring_tcpclient.h:29,
from /HttpClient.h:5,
from /HttpClient.cpp:1:
../../core-common-lib/CC3000_Host_Driver/socket.h:164:0: warning: "FD_ISSET" redefined [enabled by default]
#define FD_ISSET(fd, fdsetp) __FD_ISSET (fd, fdsetp)
^
In file included from /opt/gcc_arm/arm-none-eabi/include/stdio.h:47:0,
from ../inc/spark_wiring_print.h:30,
from ../inc/spark_wiring_client.h:23,
from ../inc/spark_wiring_tcpclient.h:29,
from /HttpClient.h:5,
from /HttpClient.cpp:1:
/opt/gcc_arm/arm-none-eabi/include/sys/types.h:260:0: note: this is the location of the previous definition
# define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1L << ((n) % NFDBITS)))
^
In file included from ../../core-common-lib/CC3000_Host_Driver/evnt_handler.h:38:0,
from ../inc/spark_wlan.h:33,
from ../inc/main.h:38,
from ../inc/spark_utilities.h:30,
from ../inc/spark_wiring.h:34,
from ../inc/spark_wiring_stream.h:36,
from ../inc/spark_wiring_client.h:24,
from ../inc/spark_wiring_tcpclient.h:29,
from /HttpClient.h:5,
from /HttpClient.cpp:1:
../../core-common-lib/CC3000_Host_Driver/socket.h:165:0: warning: "FD_ZERO" redefined [enabled by default]
#define FD_ZERO(fdsetp) __FD_ZERO (fdsetp)
^
In file included from /opt/gcc_arm/arm-none-eabi/include/stdio.h:47:0,
from ../inc/spark_wiring_print.h:30,
from ../inc/spark_wiring_client.h:23,
from ../inc/spark_wiring_tcpclient.h:29,
from /HttpClient.h:5,
from /HttpClient.cpp:1:
/opt/gcc_arm/arm-none-eabi/include/sys/types.h:261:0: note: this is the location of the previous definition
# define FD_ZERO(p) (__extension__ (void)({ \
^
In file included from ../inc/spark_wiring.h:37:0,
from ../inc/spark_wiring_stream.h:36,
from ../inc/spark_wiring_client.h:24,
from ../inc/spark_wiring_tcpclient.h:29,
from /HttpClient.h:5,
from /HttpClient.cpp:1:
../inc/spark_wiring_ipaddress.h: In member function 'IPAddress::operator uint32_t()':
../inc/spark_wiring_ipaddress.h:53:52: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
operator uint32_t() { return *((uint32_t*)_address); };
^
../inc/spark_wiring_ipaddress.h: In member function 'bool IPAddress::operator==(const IPAddress&)':
../inc/spark_wiring_ipaddress.h:54:72: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
^
../inc/spark_wiring_ipaddress.h:54:105: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
^
In file included from ../inc/spark_wiring.h:30:0,
from /application.h:6,
from /webservice-with-http-lib.cpp:1:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
In file included from ../inc/spark_wiring.h:37:0,
from /application.h:6,
from /webservice-with-http-lib.cpp:1:
../inc/spark_wiring_ipaddress.h: In member function 'IPAddress::operator uint32_t()':
../inc/spark_wiring_ipaddress.h:53:52: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
operator uint32_t() { return *((uint32_t*)_address); };
^
../inc/spark_wiring_ipaddress.h: In member function 'bool IPAddress::operator==(const IPAddress&)':
../inc/spark_wiring_ipaddress.h:54:72: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
^
../inc/spark_wiring_ipaddress.h:54:105: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
^
/HttpClient.o: In function `HttpClient::request(http_request_t&, http_response_t&, http_header_t*, char const*)':
/spark/compile_server/shared/workspace/worker_2/core-firmware/build//HttpClient.cpp:270: undefined reference to `String::operator=(String&&)'
collect2: error: ld returned 1 exit status
make: *** [10483202883edd402bd84c0334b858924f3e4690e599173dcf2a05d20f49.elf] Error 1
Error: Could not compile. Please review your code.