forked from Kong/httpsnippet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunirest.js
More file actions
55 lines (43 loc) · 1.41 KB
/
unirest.js
File metadata and controls
55 lines (43 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @description
* HTTP code snippet generator for Java using Unirest.
*
* @author
* @shashiranjan84
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/
'use strict'
var util = require('util')
var CodeBuilder = require('../../helpers/code-builder')
module.exports = function (source, options) {
var opts = util._extend({
indent: ' '
}, options)
var code = new CodeBuilder(opts.indent)
var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS' ]
if (methods.indexOf(source.method.toUpperCase()) === -1) {
code.push('HttpResponse<String> response = Unirest.customMethod("%s","%s")', source.method.toUpperCase(), source.fullUrl)
} else {
code.push('HttpResponse<String> response = Unirest.%s("%s")', source.method.toLowerCase(), source.fullUrl)
}
// Add headers, including the cookies
var headers = Object.keys(source.allHeaders)
// construct headers
if (headers.length) {
headers.forEach(function (key) {
code.push(1, '.header("%s", "%s")', key, source.allHeaders[key])
})
}
if (source.postData.text) {
code.push(1, '.body(%s)', JSON.stringify(source.postData.text))
}
code.push(1, '.asString();')
return code.join()
}
module.exports.info = {
key: 'unirest',
title: 'Unirest',
link: 'http://unirest.io/java.html',
description: 'Lightweight HTTP Request Client Library'
}