Skip to content

Commit b9ba192

Browse files
committed
updating post requests
1 parent f6f6119 commit b9ba192

File tree

2 files changed

+137
-141
lines changed

2 files changed

+137
-141
lines changed

examples/jsonget/jsonget.pde

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import http.requests.*;
22

3-
public void setup()
4-
{
5-
size(400,400);
6-
smooth();
3+
public void setup() {
4+
size(400, 400);
75

8-
GetRequest get = new GetRequest("http://dummy.restapiexample.com/api/v1/employees");
9-
get.send(); // program will wait untill the request is completed
6+
GetRequest get = new GetRequest("http://dummy.restapiexample.com/api/v1/employee/1");
7+
get.send(); // d program will wait untill the request is completed
108
println("response: " + get.getContent());
11-
129
JSONObject response = parseJSONObject(get.getContent());
1310
println("status: " + response.getString("status"));
14-
println("data: " + response.getJSONArray("data"));
11+
println("data: " + response.getJSONObject("data"));
1512
}

src/http/requests/PostRequest.java

Lines changed: 132 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -24,141 +24,140 @@
2424

2525
public class PostRequest
2626
{
27-
String url;
28-
ArrayList<BasicNameValuePair> nameValuePairs;
29-
StringEntity stringEntity;
30-
HashMap<String,File> nameFilePairs;
31-
ArrayList<BasicNameValuePair> headerPairs;
32-
33-
34-
String content;
35-
String encoding;
36-
HttpResponse response;
37-
UsernamePasswordCredentials creds;
38-
39-
public PostRequest(String url)
40-
{
41-
this(url, "ISO-8859-1");
42-
}
43-
44-
public PostRequest(String url, String encoding)
45-
{
46-
this.url = url;
47-
this.encoding = encoding;
48-
nameValuePairs = new ArrayList<BasicNameValuePair>();
49-
nameFilePairs = new HashMap<String,File>();
50-
this.headerPairs = new ArrayList<BasicNameValuePair>();
51-
}
52-
53-
public void addUser(String user, String pwd)
54-
{
55-
creds = new UsernamePasswordCredentials(user, pwd);
56-
}
57-
58-
public void addHeader(String key,String value) {
59-
BasicNameValuePair nvp = new BasicNameValuePair(key,value);
60-
headerPairs.add(nvp);
61-
62-
}
63-
64-
public void addData(String key, String value)
65-
{
66-
BasicNameValuePair nvp = new BasicNameValuePair(key,value);
67-
nameValuePairs.add(nvp);
68-
}
69-
70-
// overloads addData so you can add a JSON string:
27+
String url;
28+
ArrayList<BasicNameValuePair> nameValuePairs;
29+
StringEntity stringEntity;
30+
HashMap<String, File> nameFilePairs;
31+
ArrayList<BasicNameValuePair> headerPairs;
32+
33+
34+
String content;
35+
String encoding;
36+
HttpResponse response;
37+
UsernamePasswordCredentials creds;
38+
39+
public PostRequest(String url)
40+
{
41+
this(url, "ISO-8859-1");
42+
}
43+
44+
public PostRequest(String url, String encoding)
45+
{
46+
this.url = url;
47+
this.encoding = encoding;
48+
nameValuePairs = new ArrayList<BasicNameValuePair>();
49+
nameFilePairs = new HashMap<String, File>();
50+
this.headerPairs = new ArrayList<BasicNameValuePair>();
51+
}
52+
53+
public void addUser(String user, String pwd)
54+
{
55+
creds = new UsernamePasswordCredentials(user, pwd);
56+
}
57+
58+
public void addHeader(String key, String value) {
59+
BasicNameValuePair nvp = new BasicNameValuePair(key, value);
60+
headerPairs.add(nvp);
61+
}
62+
63+
public void addData(String key, String value)
64+
{
65+
BasicNameValuePair nvp = new BasicNameValuePair(key, value);
66+
nameValuePairs.add(nvp);
67+
}
68+
69+
// overloads addData so you can add a JSON string:
7170
public void addData(String json)
7271
{
73-
try{
74-
stringEntity = new StringEntity(json);
75-
} catch( Exception e ) {
76-
e.printStackTrace();
72+
try {
73+
stringEntity = new StringEntity(json);
74+
}
75+
catch( Exception e ) {
76+
e.printStackTrace();
7777
}
7878
}
7979

80-
public void addFile(String name, File f) {
81-
nameFilePairs.put(name,f);
82-
}
83-
84-
public void addFile(String name, String path) {
85-
File f = new File(path);
86-
nameFilePairs.put(name,f);
87-
}
88-
89-
public void send()
90-
{
91-
try {
92-
DefaultHttpClient httpClient = new DefaultHttpClient();
93-
HttpPost httpPost = new HttpPost(url);
94-
95-
if(creds != null){
96-
httpPost.addHeader(new BasicScheme().authenticate(creds, httpPost, null));
97-
}
98-
99-
if (nameFilePairs.isEmpty()) {
100-
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding));
101-
} else {
102-
MultipartEntity mentity = new MultipartEntity();
103-
Iterator<Entry<String,File>> it = nameFilePairs.entrySet().iterator();
104-
while (it.hasNext()) {
105-
Entry<String, File> pair = it.next();
106-
String name = (String) pair.getKey();
107-
File f = (File) pair.getValue();
108-
mentity.addPart(name, new FileBody(f));
109-
}
110-
for (NameValuePair nvp : nameValuePairs) {
111-
mentity.addPart(nvp.getName(), new StringBody(nvp.getValue()));
112-
}
113-
httpPost.setEntity(mentity);
114-
}
115-
116-
if (stringEntity != null) {
117-
httpPost.setEntity(stringEntity);
118-
}
119-
120-
Iterator<BasicNameValuePair> headerIterator = headerPairs.iterator();
121-
while (headerIterator.hasNext()) {
122-
BasicNameValuePair headerPair = headerIterator.next();
123-
httpPost.addHeader(headerPair.getName(),headerPair.getValue());
124-
}
125-
126-
response = httpClient.execute( httpPost );
127-
HttpEntity entity = response.getEntity();
128-
this.content = EntityUtils.toString(response.getEntity());
129-
130-
if( entity != null ) EntityUtils.consume(entity);
131-
132-
httpClient.getConnectionManager().shutdown();
133-
134-
// Clear it out for the next time
135-
nameValuePairs.clear();
136-
nameFilePairs.clear();
137-
headerPairs.clear();
138-
139-
} catch( Exception e ) {
140-
e.printStackTrace();
141-
}
142-
}
143-
144-
/* Getters
145-
_____________________________________________________________ */
146-
147-
public String getContent()
148-
{
149-
return this.content;
150-
}
151-
152-
public String getHeader(String name)
153-
{
154-
Header header = response.getFirstHeader(name);
155-
if(header == null)
156-
{
157-
return "";
158-
}
159-
else
160-
{
161-
return header.getValue();
162-
}
163-
}
164-
}
80+
public void addFile(String name, File f) {
81+
nameFilePairs.put(name, f);
82+
}
83+
84+
public void addFile(String name, String path) {
85+
File f = new File(path);
86+
nameFilePairs.put(name, f);
87+
}
88+
89+
public void send()
90+
{
91+
try {
92+
DefaultHttpClient httpClient = new DefaultHttpClient();
93+
HttpPost httpPost = new HttpPost(url);
94+
95+
if (creds != null) {
96+
httpPost.addHeader(new BasicScheme().authenticate(creds, httpPost, null));
97+
}
98+
99+
if (nameFilePairs.isEmpty()) {
100+
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding));
101+
} else {
102+
MultipartEntity mentity = new MultipartEntity();
103+
Iterator<Entry<String, File>> it = nameFilePairs.entrySet().iterator();
104+
while (it.hasNext()) {
105+
Entry<String, File> pair = it.next();
106+
String name = (String) pair.getKey();
107+
File f = (File) pair.getValue();
108+
mentity.addPart(name, new FileBody(f));
109+
}
110+
for (NameValuePair nvp : nameValuePairs) {
111+
mentity.addPart(nvp.getName(), new StringBody(nvp.getValue()));
112+
}
113+
httpPost.setEntity(mentity);
114+
}
115+
116+
if (stringEntity != null) {
117+
httpPost.setEntity(stringEntity);
118+
}
119+
120+
Iterator<BasicNameValuePair> headerIterator = headerPairs.iterator();
121+
while (headerIterator.hasNext()) {
122+
BasicNameValuePair headerPair = headerIterator.next();
123+
httpPost.addHeader(headerPair.getName(), headerPair.getValue());
124+
}
125+
126+
response = httpClient.execute( httpPost );
127+
HttpEntity entity = response.getEntity();
128+
this.content = EntityUtils.toString(response.getEntity());
129+
130+
if ( entity != null ) EntityUtils.consume(entity);
131+
132+
httpClient.getConnectionManager().shutdown();
133+
134+
// Clear it out for the next time
135+
nameValuePairs.clear();
136+
nameFilePairs.clear();
137+
headerPairs.clear();
138+
}
139+
catch( Exception e ) {
140+
e.printStackTrace();
141+
}
142+
}
143+
144+
/* Getters
145+
_____________________________________________________________ */
146+
147+
public String getContent()
148+
{
149+
return this.content;
150+
}
151+
152+
public String getHeader(String name)
153+
{
154+
Header header = response.getFirstHeader(name);
155+
if (header == null)
156+
{
157+
return "";
158+
} else
159+
{
160+
return header.getValue();
161+
}
162+
}
163+
}

0 commit comments

Comments
 (0)