Skip to content

Added POST to HttpClient #2107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion libraries/Bridge/src/HttpClient.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2013 Arduino LLC. All right reserved.
Copyright (c) 2013-2014 Arduino LLC. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -20,28 +20,62 @@

unsigned int HttpClient::get(String &url) {
begin("curl");
addHeader();
addParameter(url);
return run();
}

unsigned int HttpClient::get(const char *url) {
begin("curl");
addHeader();
addParameter(url);
return run();
}

void HttpClient::getAsynchronously(String &url) {
begin("curl");
addHeader();
addParameter(url);
runAsynchronously();
}

void HttpClient::getAsynchronously(const char *url) {
begin("curl");
addHeader();
addParameter(url);
runAsynchronously();
}

unsigned int HttpClient::post(String &url, String &data) {
return post(url.c_str(), data.c_str());
}

unsigned int HttpClient::post(const char *url, const char *data) {
begin("curl");
addParameter("--request");
addParameter("POST");
addParameter("--data");
addParameter(data);
addHeader();
addParameter(url);
return run();
}

void HttpClient::postAsynchronously(String &url, String &data) {
postAsynchronously(url.c_str(), data.c_str());
}

void HttpClient::postAsynchronously(const char *url, const char *data) {
begin("curl");
addParameter("--request");
addParameter("POST");
addParameter("--data");
addParameter(data);
addHeader();
addParameter(url);
runAsynchronously();
}

boolean HttpClient::ready() {
return running();
}
Expand All @@ -50,4 +84,18 @@ unsigned int HttpClient::getResult() {
return exitValue();
}

void HttpClient::setHeader(String &header) {
this->header = header;
}

void HttpClient::setHeader(const char * header) {
this->header = String(header);
}

void HttpClient::addHeader() {
if (header.length() > 0) {
addParameter("--header");
addParameter(header);
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove setHeader and use addHeader(name, value) or just addHeader(header). A function with add in the name implies an accumulation, and also the ability to call it more than once. Since curl actually requires that you pass multiple --headers if you want to set more than one, having addHeader(header) makes more sense (to me, at least)

11 changes: 10 additions & 1 deletion libraries/Bridge/src/HttpClient.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2013 Arduino LLC. All right reserved.
Copyright (c) 2013-2014 Arduino LLC. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -28,9 +28,18 @@ class HttpClient : public Process {
unsigned int get(const char * url);
void getAsynchronously(String &url);
void getAsynchronously(const char * url);
unsigned int post(String &url, String &data);
unsigned int post(const char * url, const char * data);
void postAsynchronously(String &url, String &data);
void postAsynchronously(const char * url, const char * data);
void setHeader(String &header);
void setHeader(const char * header);
boolean ready();
unsigned int getResult();

private:
void addHeader();
String header;
};

#endif /* HTTPCLIENT_H_ */