Skip to content

Commit 785b593

Browse files
committed
Merge branch 'master' of github.com:ahmadnassri/httpsnippet
2 parents 0182a11 + a8bd71c commit 785b593

File tree

16 files changed

+108
-0
lines changed

16 files changed

+108
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ currently the following output [targets](/src/targets) are supported:
2727
- [cURL](http://curl.haxx.se/)
2828
- [Go](http://golang.org/pkg/net/http/#NewRequest)
2929
- [HTTPie](http://httpie.org)
30+
- Java
31+
- [Unirest](http://unirest.io/java.html)
3032
- Node.js
3133
- [Native](http://nodejs.org/api/http.html#http_http_request_options_callback)
3234
- [Request](https://github.com/request/request)

src/targets/java/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = require('require-directory')(module);
4+
5+

src/targets/java/info.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = {
4+
key: 'java',
5+
title: 'JAVA',
6+
extname: '.java',
7+
default: 'unirest'
8+
};

src/targets/java/unirest.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
var util = require('util');
4+
5+
module.exports = function(options) {
6+
var self = this;
7+
var opts = util._extend({
8+
indent : ' '
9+
}, options);
10+
11+
var code = [];
12+
13+
var methods = [ "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS" ];
14+
15+
if (methods.indexOf(self.source.method.toUpperCase()) == -1) {
16+
return self.source.method.toUpperCase() + " method not supported by Unirest liabrary.";
17+
}
18+
19+
code.push(util.format('HttpResponse<String> response = Unirest.%s("%s")', self.source.method.toLowerCase(), self.source.fullUrl));
20+
21+
// Add headers, including the cookies
22+
var headers = Object.keys(self.source.allHeaders);
23+
24+
// construct headers
25+
if (headers.length) {
26+
headers.map(function(key) {
27+
code.push(util.format('.header("%s", "%s")', key, self.source.allHeaders[key]));
28+
});
29+
}
30+
31+
// construct postdata
32+
if (self.source.postData) {
33+
if (self.source.postData.text) {
34+
code.push(util.format('.body(%s)', JSON.stringify(self.source.postData.text)));
35+
}
36+
}
37+
38+
code.push(".asString();");
39+
return code.join('\n' + opts.indent);
40+
};
41+
42+
module.exports.info = {
43+
key : 'unirest',
44+
title : 'JAVA',
45+
link : 'http://unirest.io/java.html',
46+
description : 'Unirest Java interface'
47+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
2+
.header("content-type", "application/x-www-form-urlencoded")
3+
.body("foo=bar&hello=world")
4+
.asString();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
2+
.header("content-type", "application/json")
3+
.body("{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }")
4+
.asString();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
2+
.header("cookie", "foo=bar; bar=baz")
3+
.asString();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
HttpResponse<String> response = Unirest.post("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
2+
.header("cookie", "foo=bar; bar=baz")
3+
.header("accept", "application/json")
4+
.header("content-type", "application/x-www-form-urlencoded")
5+
.body("foo=bar")
6+
.asString();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
HttpResponse<String> response = Unirest.get("http://mockbin.com/har")
2+
.header("accept", "application/json")
3+
.header("x-foo", "Bar")
4+
.asString();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
HttpResponse<String> response = Unirest.post("http://mockbin.com/har")
2+
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
3+
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--")
4+
.asString();

0 commit comments

Comments
 (0)