Skip to content

Commit

Permalink
Merge pull request #45 from runemadsen/main
Browse files Browse the repository at this point in the history
default branch name changing to main
  • Loading branch information
shiffman committed Apr 23, 2021
2 parents f6f6119 + 7e2b35f commit da4cc48
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 143 deletions.
13 changes: 5 additions & 8 deletions examples/jsonget/jsonget.pde
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import http.requests.*;

public void setup()
{
size(400,400);
smooth();
public void setup() {
size(400, 400);

GetRequest get = new GetRequest("http://dummy.restapiexample.com/api/v1/employees");
get.send(); // program will wait untill the request is completed
GetRequest get = new GetRequest("http://dummy.restapiexample.com/api/v1/employee/1");
get.send(); // d program will wait untill the request is completed
println("response: " + get.getContent());

JSONObject response = parseJSONObject(get.getContent());
println("status: " + response.getString("status"));
println("data: " + response.getJSONArray("data"));
println("data: " + response.getJSONObject("data"));
}
4 changes: 2 additions & 2 deletions resources/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ source.repository=https://github.com/runemadsen/HTTProcessing
# This is used to compare different versions of the same library, and check if
# an update is available.

library.version=3
library.version=4


# The version as the user will see it.

library.prettyVersion=0.1.3
library.prettyVersion=0.1.4


library.copyright=(c) 2014
Expand Down
265 changes: 132 additions & 133 deletions src/http/requests/PostRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,141 +24,140 @@

public class PostRequest
{
String url;
ArrayList<BasicNameValuePair> nameValuePairs;
StringEntity stringEntity;
HashMap<String,File> nameFilePairs;
ArrayList<BasicNameValuePair> headerPairs;


String content;
String encoding;
HttpResponse response;
UsernamePasswordCredentials creds;

public PostRequest(String url)
{
this(url, "ISO-8859-1");
}

public PostRequest(String url, String encoding)
{
this.url = url;
this.encoding = encoding;
nameValuePairs = new ArrayList<BasicNameValuePair>();
nameFilePairs = new HashMap<String,File>();
this.headerPairs = new ArrayList<BasicNameValuePair>();
}

public void addUser(String user, String pwd)
{
creds = new UsernamePasswordCredentials(user, pwd);
}

public void addHeader(String key,String value) {
BasicNameValuePair nvp = new BasicNameValuePair(key,value);
headerPairs.add(nvp);

}

public void addData(String key, String value)
{
BasicNameValuePair nvp = new BasicNameValuePair(key,value);
nameValuePairs.add(nvp);
}

// overloads addData so you can add a JSON string:
String url;
ArrayList<BasicNameValuePair> nameValuePairs;
StringEntity stringEntity;
HashMap<String, File> nameFilePairs;
ArrayList<BasicNameValuePair> headerPairs;


String content;
String encoding;
HttpResponse response;
UsernamePasswordCredentials creds;

public PostRequest(String url)
{
this(url, "ISO-8859-1");
}

public PostRequest(String url, String encoding)
{
this.url = url;
this.encoding = encoding;
nameValuePairs = new ArrayList<BasicNameValuePair>();
nameFilePairs = new HashMap<String, File>();
this.headerPairs = new ArrayList<BasicNameValuePair>();
}

public void addUser(String user, String pwd)
{
creds = new UsernamePasswordCredentials(user, pwd);
}

public void addHeader(String key, String value) {
BasicNameValuePair nvp = new BasicNameValuePair(key, value);
headerPairs.add(nvp);
}

public void addData(String key, String value)
{
BasicNameValuePair nvp = new BasicNameValuePair(key, value);
nameValuePairs.add(nvp);
}

// overloads addData so you can add a JSON string:
public void addData(String json)
{
try{
stringEntity = new StringEntity(json);
} catch( Exception e ) {
e.printStackTrace();
try {
stringEntity = new StringEntity(json);
}
catch( Exception e ) {
e.printStackTrace();
}
}

public void addFile(String name, File f) {
nameFilePairs.put(name,f);
}

public void addFile(String name, String path) {
File f = new File(path);
nameFilePairs.put(name,f);
}

public void send()
{
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

if(creds != null){
httpPost.addHeader(new BasicScheme().authenticate(creds, httpPost, null));
}

if (nameFilePairs.isEmpty()) {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding));
} else {
MultipartEntity mentity = new MultipartEntity();
Iterator<Entry<String,File>> it = nameFilePairs.entrySet().iterator();
while (it.hasNext()) {
Entry<String, File> pair = it.next();
String name = (String) pair.getKey();
File f = (File) pair.getValue();
mentity.addPart(name, new FileBody(f));
}
for (NameValuePair nvp : nameValuePairs) {
mentity.addPart(nvp.getName(), new StringBody(nvp.getValue()));
}
httpPost.setEntity(mentity);
}

if (stringEntity != null) {
httpPost.setEntity(stringEntity);
}

Iterator<BasicNameValuePair> headerIterator = headerPairs.iterator();
while (headerIterator.hasNext()) {
BasicNameValuePair headerPair = headerIterator.next();
httpPost.addHeader(headerPair.getName(),headerPair.getValue());
}

response = httpClient.execute( httpPost );
HttpEntity entity = response.getEntity();
this.content = EntityUtils.toString(response.getEntity());

if( entity != null ) EntityUtils.consume(entity);

httpClient.getConnectionManager().shutdown();

// Clear it out for the next time
nameValuePairs.clear();
nameFilePairs.clear();
headerPairs.clear();

} catch( Exception e ) {
e.printStackTrace();
}
}

/* Getters
_____________________________________________________________ */

public String getContent()
{
return this.content;
}

public String getHeader(String name)
{
Header header = response.getFirstHeader(name);
if(header == null)
{
return "";
}
else
{
return header.getValue();
}
}
}
public void addFile(String name, File f) {
nameFilePairs.put(name, f);
}

public void addFile(String name, String path) {
File f = new File(path);
nameFilePairs.put(name, f);
}

public void send()
{
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

if (creds != null) {
httpPost.addHeader(new BasicScheme().authenticate(creds, httpPost, null));
}

if (nameFilePairs.isEmpty()) {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding));
} else {
MultipartEntity mentity = new MultipartEntity();
Iterator<Entry<String, File>> it = nameFilePairs.entrySet().iterator();
while (it.hasNext()) {
Entry<String, File> pair = it.next();
String name = (String) pair.getKey();
File f = (File) pair.getValue();
mentity.addPart(name, new FileBody(f));
}
for (NameValuePair nvp : nameValuePairs) {
mentity.addPart(nvp.getName(), new StringBody(nvp.getValue()));
}
httpPost.setEntity(mentity);
}

if (stringEntity != null) {
httpPost.setEntity(stringEntity);
}

Iterator<BasicNameValuePair> headerIterator = headerPairs.iterator();
while (headerIterator.hasNext()) {
BasicNameValuePair headerPair = headerIterator.next();
httpPost.addHeader(headerPair.getName(), headerPair.getValue());
}

response = httpClient.execute( httpPost );
HttpEntity entity = response.getEntity();
this.content = EntityUtils.toString(response.getEntity());

if ( entity != null ) EntityUtils.consume(entity);

httpClient.getConnectionManager().shutdown();

// Clear it out for the next time
nameValuePairs.clear();
nameFilePairs.clear();
headerPairs.clear();
}
catch( Exception e ) {
e.printStackTrace();
}
}

/* Getters
_____________________________________________________________ */

public String getContent()
{
return this.content;
}

public String getHeader(String name)
{
Header header = response.getFirstHeader(name);
if (header == null)
{
return "";
} else
{
return header.getValue();
}
}
}

0 comments on commit da4cc48

Please sign in to comment.