From b9ba19240b9a4967a9f9896c5f4351cd342ae8a1 Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Fri, 23 Apr 2021 10:54:44 -0400 Subject: [PATCH 1/2] updating post requests --- examples/jsonget/jsonget.pde | 13 +- src/http/requests/PostRequest.java | 265 ++++++++++++++--------------- 2 files changed, 137 insertions(+), 141 deletions(-) diff --git a/examples/jsonget/jsonget.pde b/examples/jsonget/jsonget.pde index 2f76cd2..0b2250f 100644 --- a/examples/jsonget/jsonget.pde +++ b/examples/jsonget/jsonget.pde @@ -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")); } diff --git a/src/http/requests/PostRequest.java b/src/http/requests/PostRequest.java index ce23198..01ca7d1 100644 --- a/src/http/requests/PostRequest.java +++ b/src/http/requests/PostRequest.java @@ -24,141 +24,140 @@ public class PostRequest { - String url; - ArrayList nameValuePairs; - StringEntity stringEntity; - HashMap nameFilePairs; - ArrayList 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(); - nameFilePairs = new HashMap(); - this.headerPairs = new ArrayList(); - } - - 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 nameValuePairs; + StringEntity stringEntity; + HashMap nameFilePairs; + ArrayList 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(); + nameFilePairs = new HashMap(); + this.headerPairs = new ArrayList(); + } + + 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> it = nameFilePairs.entrySet().iterator(); - while (it.hasNext()) { - Entry 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 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> it = nameFilePairs.entrySet().iterator(); + while (it.hasNext()) { + Entry 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 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(); + } + } +} \ No newline at end of file From 7e2b35fb9e3dce94e1c370fb70ea62832e025bcb Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Fri, 23 Apr 2021 10:54:53 -0400 Subject: [PATCH 2/2] updating version numbers --- resources/build.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/build.properties b/resources/build.properties index 2cdad27..f21be63 100644 --- a/resources/build.properties +++ b/resources/build.properties @@ -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