forked from postmanlabs/httpsnippet-fsless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp2.js
More file actions
129 lines (106 loc) · 3.34 KB
/
http2.js
File metadata and controls
129 lines (106 loc) · 3.34 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* @description
* HTTP code snippet generator for PHP using curl-ext.
*
* @author
* @AhmadNassri
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/
'use strict'
var util = require('util')
var helpers = require('./helpers')
var CodeBuilder = require('../../helpers/code-builder')
module.exports = function (source, options) {
var opts = util._extend({
closingTag: false,
indent: ' ',
noTags: false,
shortTags: false
}, options)
var code = new CodeBuilder(opts.indent)
var hasBody = false
if (!opts.noTags) {
code.push(opts.shortTags ? '<?' : '<?php')
.blank()
}
code.push('$client = new http\\Client;')
.push('$request = new http\\Client\\Request;')
.blank()
switch (source.postData.mimeType) {
case 'application/x-www-form-urlencoded':
code.push('$body = new http\\Message\\Body;')
.push('$body->append(new http\\QueryString(%s));', helpers.convert(source.postData.paramsObj, opts.indent))
.blank()
hasBody = true
break
case 'multipart/form-data':
var files = []
var fields = {}
source.postData.params.forEach(function (param) {
if (param.fileName) {
files.push({
name: param.name,
type: param.contentType,
file: param.fileName,
data: param.value
})
} else if (param.value) {
fields[param.name] = param.value
}
})
code.push('$body = new http\\Message\\Body;')
.push('$body->addForm(%s, %s);',
Object.keys(fields).length ? helpers.convert(fields, opts.indent) : 'NULL',
files.length ? helpers.convert(files, opts.indent) : 'NULL'
)
// remove the contentType header
if (~source.headersObj['content-type'].indexOf('boundary')) {
delete source.headersObj['content-type']
}
code.blank()
hasBody = true
break
default:
if (source.postData.text) {
code.push('$body = new http\\Message\\Body;')
.push('$body->append(%s);', helpers.convert(source.postData.text))
.blank()
hasBody = true
}
}
code.push('$request->setRequestUrl(%s);', helpers.convert(source.url))
.push('$request->setRequestMethod(%s);', helpers.convert(source.method))
if (hasBody) {
code.push('$request->setBody($body);')
.blank()
}
if (Object.keys(source.queryObj).length) {
code.push('$request->setQuery(new http\\QueryString(%s));', helpers.convert(source.queryObj, opts.indent))
.blank()
}
if (Object.keys(source.headersObj).length) {
code.push('$request->setHeaders(%s);', helpers.convert(source.headersObj, opts.indent))
.blank()
}
if (Object.keys(source.cookiesObj).length) {
code.blank()
.push('$client->setCookies(%s);', helpers.convert(source.cookiesObj, opts.indent))
.blank()
}
code.push('$client->enqueue($request)->send();')
.push('$response = $client->getResponse();')
.blank()
.push('echo $response->getBody();')
if (!opts.noTags && opts.closingTag) {
code.blank()
.push('?>')
}
return code.join()
}
module.exports.info = {
key: 'http2',
title: 'HTTP v2',
link: 'http://devel-m6w6.rhcloud.com/mdref/http',
description: 'PHP with pecl/http v2'
}