Skip to content

Commit c7e5e7c

Browse files
markFromMSTFederico Fissore
authored and
Federico Fissore
committed
Added POST to HttpClient
Added POST to HttpClient and also the ability to set a header (for API Keys) for GET and POST.
1 parent 5de6192 commit c7e5e7c

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

Diff for: libraries/Bridge/src/HttpClient.cpp

+56-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2013 Arduino LLC. All right reserved.
2+
Copyright (c) 2013-2014 Arduino LLC. All right reserved.
33
44
This library is free software; you can redistribute it and/or
55
modify it under the terms of the GNU Lesser General Public
@@ -28,6 +28,7 @@ unsigned int HttpClient::get(String &url) {
2828
if (insecure) {
2929
addParameter("-k");
3030
}
31+
addHeader();
3132
addParameter(url);
3233
return run();
3334
}
@@ -37,6 +38,7 @@ unsigned int HttpClient::get(const char *url) {
3738
if (insecure) {
3839
addParameter("-k");
3940
}
41+
addHeader();
4042
addParameter(url);
4143
return run();
4244
}
@@ -46,6 +48,7 @@ void HttpClient::getAsynchronously(String &url) {
4648
if (insecure) {
4749
addParameter("-k");
4850
}
51+
addHeader();
4952
addParameter(url);
5053
runAsynchronously();
5154
}
@@ -55,6 +58,43 @@ void HttpClient::getAsynchronously(const char *url) {
5558
if (insecure) {
5659
addParameter("-k");
5760
}
61+
addHeader();
62+
addParameter(url);
63+
runAsynchronously();
64+
}
65+
66+
unsigned int HttpClient::post(String &url, String &data) {
67+
return post(url.c_str(), data.c_str());
68+
}
69+
70+
unsigned int HttpClient::post(const char *url, const char *data) {
71+
begin("curl");
72+
if (insecure) {
73+
addParameter("-k");
74+
}
75+
addParameter("--request");
76+
addParameter("POST");
77+
addParameter("--data");
78+
addParameter(data);
79+
addHeader();
80+
addParameter(url);
81+
return run();
82+
}
83+
84+
void HttpClient::postAsynchronously(String &url, String &data) {
85+
postAsynchronously(url.c_str(), data.c_str());
86+
}
87+
88+
void HttpClient::postAsynchronously(const char *url, const char *data) {
89+
begin("curl");
90+
if (insecure) {
91+
addParameter("-k");
92+
}
93+
addParameter("--request");
94+
addParameter("POST");
95+
addParameter("--data");
96+
addParameter(data);
97+
addHeader();
5898
addParameter(url);
5999
runAsynchronously();
60100
}
@@ -75,3 +115,18 @@ void HttpClient::checkSSL() {
75115
insecure = false;
76116
}
77117

118+
void HttpClient::setHeader(String &header) {
119+
this->header = header;
120+
}
121+
122+
void HttpClient::setHeader(const char * header) {
123+
this->header = String(header);
124+
}
125+
126+
void HttpClient::addHeader() {
127+
if (header.length() > 0) {
128+
addParameter("--header");
129+
addParameter(header);
130+
}
131+
}
132+

Diff for: libraries/Bridge/src/HttpClient.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2013 Arduino LLC. All right reserved.
2+
Copyright (c) 2013-2014 Arduino LLC. All right reserved.
33
44
This library is free software; you can redistribute it and/or
55
modify it under the terms of the GNU Lesser General Public
@@ -29,6 +29,12 @@ class HttpClient : public Process {
2929
unsigned int get(const char * url);
3030
void getAsynchronously(String &url);
3131
void getAsynchronously(const char * url);
32+
unsigned int post(String &url, String &data);
33+
unsigned int post(const char * url, const char * data);
34+
void postAsynchronously(String &url, String &data);
35+
void postAsynchronously(const char * url, const char * data);
36+
void setHeader(String &header);
37+
void setHeader(const char * header);
3238
boolean ready();
3339
unsigned int getResult();
3440
void noCheckSSL();
@@ -37,6 +43,9 @@ class HttpClient : public Process {
3743
private:
3844
boolean insecure;
3945

46+
private:
47+
void addHeader();
48+
String header;
4049
};
4150

4251
#endif /* HTTPCLIENT_H_ */

0 commit comments

Comments
 (0)