Skip to content

Commit

Permalink
Merge pull request #14 from nullboundary/master
Browse files Browse the repository at this point in the history
addHeader method for get and post requests
  • Loading branch information
runemadsen committed Sep 22, 2015
2 parents 5007932 + 482bb54 commit 3fe3480
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 20 deletions.
42 changes: 24 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,30 @@ How to
Install the library by [downloading the latest release](https://github.com/runemadsen/HTTProcessing/releases) or via the [Processing contribution manager](http://wiki.processing.org/w/How_to_Install_a_Contributed_Library).

Then import the library in your sketch:

import http.requests.*;

```Java
import http.requests.*;
```
Then you can make GET and POST requests from your code:

GetRequest get = new GetRequest("http://httprocessing.heroku.com");
get.send();
println("Reponse Content: " + get.getContent());
println("Reponse Content-Length Header: " + get.getHeader("Content-Length"));
```Java
GetRequest get = new GetRequest("http://httprocessing.heroku.com");
get.send();
println("Reponse Content: " + get.getContent());
println("Reponse Content-Length Header: " + get.getHeader("Content-Length"));

PostRequest post = new PostRequest("http://httprocessing.heroku.com");
post.addData("name", "Rune");
post.send();
println("Reponse Content: " + post.getContent());
println("Reponse Content-Length Header: " + post.getHeader("Content-Length"));

PostRequest post = new PostRequest("http://httprocessing.heroku.com");
post.addData("name", "Rune");
post.send();
println("Reponse Content: " + post.getContent());
println("Reponse Content-Length Header: " + post.getHeader("Content-Length"));
```
To authenticate requests using a Basic Access authentication scheme, include the following in your requests:

get.addUser("username", "password");

post.addUser("username", "password");
```Java
get.addUser("username", "password");
post.addUser("username", "password");
```
To add a header to your request, including the following:
```Java
//method: addHeader(name,value)
get.addHeader("Accept", "application/json");
post.addHeader("Content-Type", "application/json");
```
27 changes: 25 additions & 2 deletions src/http/requests/GetRequest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package http.requests;

import java.util.ArrayList;
import java.util.Iterator;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class GetRequest
Expand All @@ -15,17 +20,28 @@ public class GetRequest
String content;
HttpResponse response;
UsernamePasswordCredentials creds;
ArrayList<BasicNameValuePair> headerPairs;


public GetRequest(String url)
{
this.url = url;
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 send()
{
try {
Expand All @@ -37,6 +53,13 @@ public void send()
httpGet.addHeader(new BasicScheme().authenticate(creds, httpGet, null));
}

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


response = httpClient.execute( httpGet );
HttpEntity entity = response.getEntity();
this.content = EntityUtils.toString(response.getEntity());
Expand Down
14 changes: 14 additions & 0 deletions src/http/requests/PostRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class PostRequest
String url;
ArrayList<BasicNameValuePair> nameValuePairs;
HashMap<String,File> nameFilePairs;
ArrayList<BasicNameValuePair> headerPairs;


String content;
String encoding;
Expand All @@ -49,6 +51,12 @@ 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)
{
Expand Down Expand Up @@ -92,6 +100,12 @@ public void send()
httpPost.setEntity(mentity);
}

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());
Expand Down

0 comments on commit 3fe3480

Please sign in to comment.