Skip to content

Commit 3fe3480

Browse files
committed
Merge pull request #14 from nullboundary/master
addHeader method for get and post requests
2 parents 5007932 + 482bb54 commit 3fe3480

File tree

3 files changed

+63
-20
lines changed

3 files changed

+63
-20
lines changed

README.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,30 @@ How to
1111
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).
1212

1313
Then import the library in your sketch:
14-
15-
import http.requests.*;
16-
14+
```Java
15+
import http.requests.*;
16+
```
1717
Then you can make GET and POST requests from your code:
18-
19-
GetRequest get = new GetRequest("http://httprocessing.heroku.com");
20-
get.send();
21-
println("Reponse Content: " + get.getContent());
22-
println("Reponse Content-Length Header: " + get.getHeader("Content-Length"));
18+
```Java
19+
GetRequest get = new GetRequest("http://httprocessing.heroku.com");
20+
get.send();
21+
println("Reponse Content: " + get.getContent());
22+
println("Reponse Content-Length Header: " + get.getHeader("Content-Length"));
2323

24-
PostRequest post = new PostRequest("http://httprocessing.heroku.com");
25-
post.addData("name", "Rune");
26-
post.send();
27-
println("Reponse Content: " + post.getContent());
28-
println("Reponse Content-Length Header: " + post.getHeader("Content-Length"));
29-
24+
PostRequest post = new PostRequest("http://httprocessing.heroku.com");
25+
post.addData("name", "Rune");
26+
post.send();
27+
println("Reponse Content: " + post.getContent());
28+
println("Reponse Content-Length Header: " + post.getHeader("Content-Length"));
29+
```
3030
To authenticate requests using a Basic Access authentication scheme, include the following in your requests:
31-
32-
get.addUser("username", "password");
33-
34-
post.addUser("username", "password");
31+
```Java
32+
get.addUser("username", "password");
33+
post.addUser("username", "password");
34+
```
35+
To add a header to your request, including the following:
36+
```Java
37+
//method: addHeader(name,value)
38+
get.addHeader("Accept", "application/json");
39+
post.addHeader("Content-Type", "application/json");
40+
```

src/http/requests/GetRequest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package http.requests;
22

3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
36
import org.apache.http.Header;
47
import org.apache.http.HttpEntity;
58
import org.apache.http.HttpResponse;
9+
import org.apache.http.NameValuePair;
610
import org.apache.http.auth.UsernamePasswordCredentials;
711
import org.apache.http.client.methods.HttpGet;
812
import org.apache.http.impl.auth.BasicScheme;
913
import org.apache.http.impl.client.DefaultHttpClient;
14+
import org.apache.http.message.BasicNameValuePair;
1015
import org.apache.http.util.EntityUtils;
1116

1217
public class GetRequest
@@ -15,17 +20,28 @@ public class GetRequest
1520
String content;
1621
HttpResponse response;
1722
UsernamePasswordCredentials creds;
23+
ArrayList<BasicNameValuePair> headerPairs;
1824

25+
1926
public GetRequest(String url)
2027
{
2128
this.url = url;
29+
headerPairs = new ArrayList<BasicNameValuePair>();
30+
2231
}
2332

2433
public void addUser(String user, String pwd)
2534
{
2635
creds = new UsernamePasswordCredentials(user, pwd);
27-
}
28-
36+
37+
}
38+
39+
public void addHeader(String key,String value) {
40+
BasicNameValuePair nvp = new BasicNameValuePair(key,value);
41+
headerPairs.add(nvp);
42+
43+
}
44+
2945
public void send()
3046
{
3147
try {
@@ -37,6 +53,13 @@ public void send()
3753
httpGet.addHeader(new BasicScheme().authenticate(creds, httpGet, null));
3854
}
3955

56+
Iterator<BasicNameValuePair> headerIterator = headerPairs.iterator();
57+
while (headerIterator.hasNext()) {
58+
BasicNameValuePair headerPair = headerIterator.next();
59+
httpGet.addHeader(headerPair.getName(),headerPair.getValue());
60+
}
61+
62+
4063
response = httpClient.execute( httpGet );
4164
HttpEntity entity = response.getEntity();
4265
this.content = EntityUtils.toString(response.getEntity());

src/http/requests/PostRequest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class PostRequest
2626
String url;
2727
ArrayList<BasicNameValuePair> nameValuePairs;
2828
HashMap<String,File> nameFilePairs;
29+
ArrayList<BasicNameValuePair> headerPairs;
30+
2931

3032
String content;
3133
String encoding;
@@ -49,6 +51,12 @@ public void addUser(String user, String pwd)
4951
{
5052
creds = new UsernamePasswordCredentials(user, pwd);
5153
}
54+
55+
public void addHeader(String key,String value) {
56+
BasicNameValuePair nvp = new BasicNameValuePair(key,value);
57+
headerPairs.add(nvp);
58+
59+
}
5260

5361
public void addData(String key, String value)
5462
{
@@ -92,6 +100,12 @@ public void send()
92100
httpPost.setEntity(mentity);
93101
}
94102

103+
Iterator<BasicNameValuePair> headerIterator = headerPairs.iterator();
104+
while (headerIterator.hasNext()) {
105+
BasicNameValuePair headerPair = headerIterator.next();
106+
httpPost.addHeader(headerPair.getName(),headerPair.getValue());
107+
}
108+
95109
response = httpClient.execute( httpPost );
96110
HttpEntity entity = response.getEntity();
97111
this.content = EntityUtils.toString(response.getEntity());

0 commit comments

Comments
 (0)