Socket
Socket
Sign inDemoInstall

httpsnippet

Package Overview
Dependencies
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

httpsnippet - npm Package Compare versions

Comparing version 1.0.0-beta.10 to 1.0.0

src/targets/node/unirest.js

275

CONTRIBUTING.md

@@ -136,1 +136,276 @@ # Contributing to this project

license your work under the same license as that used by the project.
## Creating New Conversion Targets
Please start by browsing for [available targets](/src/targets) and inspect each implementation.
a target is a simple module with a constructor that accepts two parameters: `source` and `options`,
where `source` is the HAR Object to process, and `options` is an optional object with any target
specific flags *(used for customizing the output)*.
### Conversion Rules
1. start by reading an understanding the [HAR](http://www.softwareishard.com/blog/har-12-spec/#request) format.
2. utilize utility properties created for convenience (`source.headersObj`, `source.uriObj` etc ...) *see below for mode details*
3. follow the guidelines below for best practices and consistency.
### Guidelines
Using the following example of a request object, HTTP Snippet will pre-process data and create some additional properties:
| property | description |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `source.fullUrl` | the full & final url, including all query string values |
| `source.uriObj` | the url parsed with `url.parse()`. compatible with `url.format` |
| `source.queryObj` | a key => value pair, "normalized" version of `source.queryString`, adds additional query string values from the `source.url` |
| `source.headersObj` | a key => value pair, "normalized" version of `source.headers`, header names are lowercased |
| `source.allHeaders` | same as `source.headersObj` but with `cookies` header and populated from `source.cookies` array |
| `source.postData.jsonObj` | the parsed value of `source.postData.text`, only for `source.postData.mimeType` = `application/json` *(or equivalent mimeTypes)* |
| `source.postData.paramsObj` | a key => value pair, "normalized" version of `source.postData.params`, only for `source.postData.mimeType` = `application/x-www-form-urlencoded` |
###### Sample Incoming Request Object
```js
{
method: 'POST',
url: 'http://mockbin.com/har?key=value',
httpVersion: 'HTTP/1.1',
queryString: [
{ name: 'foo', value: 'bar' },
{ name: 'foo', value: 'baz' },
{ name: 'baz', value: 'abc' }
],
headers: [
{ name: 'Accept', value: 'application/json' },
{ name: 'Content-Type', value: 'application/x-www-form-urlencoded' }
],
cookies: [
{ name: 'foo', value: 'bar' },
{ name: 'bar', value: 'baz' }
],
postData: {
mimeType: 'application/x-www-form-urlencoded',
params: [
{ name: 'foo', value: 'bar' },
{ name: 'foo', value: 'baz' },
{ name: 'baz', value: 'abc' }
]
}
}
```
###### Processed Source Object
```js
{
method: 'POST',
// the base url value stripped of any the query string
url: 'http://mockbin.com/har',
// the full & final url, including all query string values
fullUrl: 'http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value',
// the url parsed with url.parse()
// compatible with url.format
uriObj: {
protocol: 'http:',
slashes: true,
auth: null,
host: 'mockbin.com',
port: null,
hostname: 'mockbin.com',
hash: null,
search: 'key=value&baz=abc&foo=bar&foo=baz',
query: { key: 'value', baz: 'abc', foo: [Object] },
pathname: '/har',
path: '/har?key=value&baz=abc&foo=bar&foo=baz',
href: 'http://mockbin.com/har'
},
httpVersion: 'HTTP/1.1',
// added to pass har-validator
bodySize: 0,
// added to pass har-validator
headersSize: 0,
queryString: [
{ name: 'foo', value: 'bar' },
{ name: 'foo', value: 'baz' },
{ name: 'baz', value: 'abc' }
],
// "normalized" version of `queryString`
// adds any additional query string values from the url
// compatible with "querystring" node module
queryObj: {
key: 'value',
baz: 'abc',
foo: [ 'bar', 'baz' ]
},
headers: [
{ name: 'Accept', value: 'application/json' },
{ name: 'Content-Type', value: 'application/x-www-form-urlencoded' }
],
// normalized headers array into a key => value object pair
// header names are lowercased
headersObj: {
'accept': 'application/json',
'content-type': 'application/x-www-form-urlencoded'
},
// same as headersObj but with Cookies added (if any exist in cookies array)
allHeaders: {
'accept': 'application/json',
'content-type': 'application/x-www-form-urlencoded',
'cookie': 'foo=bar; bar=baz'
},
cookies: [
{ name: 'foo', value: 'bar' },
{ name: 'bar', value: 'baz' }
],
// see below for different scenarios
postData: [Object]
}
```
###### application/x-www-form-urlencoded
```js
postData: {
// added to pass har-validator
size: 0,
// original value
mimeType: 'application/x-www-form-urlencoded',
// original value
params: [
{ name: 'foo', value: 'bar' },
{ name: 'foo', value: 'baz' },
{ name: 'baz', value: 'abc' }
],
// "normalized" version of `params`
// compatible with "querystring" node module
paramsObj: {
key: 'value',
baz: 'abc',
foo: [ 'bar', 'baz' ]
}
// the raw body in plain text
// this value will be always overwritten in this scenario
text: 'baz=abc&foo=bar&foo=baz'
// see below
jsonObj: false
}
```
###### application/json
- will match when `postData.mimeType` is one of: `application/json`, `text/json`, `text/x-json`, `application/x-json`
- In case of failure to parse `postData.text` as a JSON object, `postData.mimeType` is set to `text/plain`, `postData.jsonObj` remains as `false`. this is done so that the implementing target, would still attempt to post the raw body as is.
- This also emphasizes not to rely on `postData.mimeType` for the `Content-Type` header!
```js
postData: {
// added to pass har-validator
size: 0,
// original value
mimeType: 'application/json',
// ignored
params: [],
// default value
paramsObj: false
// the raw body in plain text
text: '"{\"foo\": \"bar\"}"'
// the parsed value of postData.text
jsonObj: {
foo: 'bar'
}
}
```
###### multipart/form-data
- will match when `postData.mimeType` is one of: `multipart/mixed` `multipart/related`, `multipart/form-data`, `multipart/alternative`
- will force `postData.mimeType` to `multipart/form-data`
- will create/overwrite the `Content-Type` header if it does not exist, with the appropriate boundary flag.
- when no `params[].value` is present, will default to empty content
```js
postData: {
// added to pass har-validator
size: 0,
// original value
mimeType: 'multipart/form-data',
// parsed into text values
params: [
{
name: 'foo',
value: 'bar'
}
]
// ignored
paramsObj: false
// the raw body in plain text
// generated based on appropriately parsing the `params` into a multi-boundary content string
// this value will be always overwritten in this scenario
text: '----------------------------591447866569479977899212\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n----------------------------591447866569479977899212--'
// ignored
jsonObj: false
}
```
###### multipart/form-data (File Uploads)
```js
postData: {
// added to pass har-validator
size: 0,
// original value
mimeType: 'multipart/form-data',
// parsed into text values
params: [
{
name: 'foo',
value: 'Hello World',
fileName: 'test/fixtures/files/hello.txt',
contentType: 'text/plain'
}
]
// ignored
paramsObj: false
// the raw body in plain text
// generated based on appropriately parsing the `params` into a multi-boundary content string
// this value will be always overwritten in this scenario
text: '----------------------------771333709043252625002993\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n----------------------------771333709043252625002993--'
// ignored
jsonObj: false
}
```

3

package.json
{
"version": "1.0.0-beta.10",
"version": "1.0.0",
"name": "httpsnippet",

@@ -43,2 +43,3 @@ "description": "HTTP Request snippet generator for *most* languages",

"debug": "^2.1.1",
"form-data": "^0.2.0",
"har-validator": "^1.1.2",

@@ -45,0 +46,0 @@ "require-directory": "^2.1.0"

@@ -31,2 +31,5 @@ # HTTP Snippet [![version][npm-version]][npm-url] [![License][npm-license]][license-url]

- [Native](http://nodejs.org/api/http.html#http_http_request_options_callback)
- [Request](https://github.com/request/request)
- OCaml
- [CoHTTP](https://github.com/mirage/ocaml-cohttp)
- PHP

@@ -33,0 +36,0 @@ - [ext-curl](http://php.net/manual/en/book.curl.php)

'use strict';
var debug = require('debug')('httpsnippet');
var es = require('event-stream');
var FormData = require('form-data');
var qs = require('querystring');
var reducer = require('./reducer');
var qs = require('querystring');
var targets = require('./targets');
var url = require('url');
var util = require('util');
var validate = require('har-validator');
var util = require('util');

@@ -35,6 +37,59 @@ // constructor

this.source.headersObj = {};
this.source.postData.jsonObj = {};
this.source.postData.paramsObj = {};
this.source.allHeaders = {};
this.source.postData.jsonObj = false;
this.source.postData.paramsObj = false;
// construct query objects
if (this.source.queryString && this.source.queryString.length) {
debug('queryString found, constructing queryString pair map');
this.source.queryObj = this.source.queryString.reduce(reducer, {});
}
// construct headers objects
if (this.source.headers && this.source.headers.length) {
// loweCase header keys
this.source.headersObj = this.source.headers.reduceRight(function (headers, header) {
headers[header.name.toLowerCase()] = header.value;
return headers;
}, {});
}
// construct Cookie header
var cookies = this.source.cookies.map(function (cookie) {
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
});
if (cookies.length) {
this.source.allHeaders.cookie = cookies.join('; ');
}
switch (this.source.postData.mimeType) {
case 'multipart/mixed':
case 'multipart/related':
case 'multipart/form-data':
case 'multipart/alternative':
// reset values
this.source.postData.text = '';
this.source.postData.mimeType = 'multipart/form-data';
var form = new FormData();
// easter egg
form._boundary = '---011000010111000001101001';
this.source.postData.params.map(function (param) {
form.append(param.name, param.value || '', {
filename: param.fileName || null,
contentType: param.contentType || null
});
});
form.pipe(es.map(function (data, cb) {
this.source.postData.text += data;
}.bind(this)));
this.source.headersObj['content-type'] = 'multipart/form-data; boundary=' + form.getBoundary();
break;
case 'application/x-www-form-urlencoded':

@@ -51,5 +106,18 @@ if (!this.source.postData.params) {

case 'text/json':
case 'text/x-json':
case 'application/json':
case 'application/x-json':
this.source.postData.mimeType = 'application/json';
if (this.source.postData.text) {
this.source.postData.jsonObj = JSON.parse(this.source.postData.text);
try {
this.source.postData.jsonObj = JSON.parse(this.source.postData.text);
} catch (e) {
debug(e);
// force back to text/plain
// if headers have proper content-type value, then this should also work
this.source.postData.mimeType = 'text/plain';
}
}

@@ -59,16 +127,5 @@ break;

// construct query objects
if (this.source.queryString && this.source.queryString.length) {
debug('queryString found, constructing queryString pair map');
// create allHeaders object
this.source.allHeaders = util._extend(this.source.allHeaders, this.source.headersObj);
this.source.queryObj = this.source.queryString.reduce(reducer, {});
}
// construct headers objects
if (this.source.headers && this.source.headers.length) {
debug('headers found, constructing header pair map');
this.source.headersObj = this.source.headers.reduce(reducer, {});
}
// deconstruct the uri

@@ -78,3 +135,3 @@ this.source.uriObj = url.parse(this.source.url, true, true);

// merge all possible queryString values
this.source.queryObj = this.source.queryString = util._extend(this.source.uriObj.query, this.source.queryObj);
this.source.queryObj = util._extend(this.source.queryObj, this.source.uriObj.query);

@@ -90,4 +147,4 @@ // reset uriObj values for a clean url

// update the uri object
this.source.uriObj.query = this.source.queryString;
this.source.uriObj.search = qs.stringify(this.source.queryString);
this.source.uriObj.query = this.source.queryObj;
this.source.uriObj.search = qs.stringify(this.source.queryObj);
this.source.uriObj.path = this.source.uriObj.pathname + '?' + this.source.uriObj.search;

@@ -108,3 +165,3 @@

if (func) {
return func.call(this, opts);
return func.call(this, this.source, opts);
}

@@ -111,0 +168,0 @@

@@ -5,3 +5,3 @@ 'use strict';

module.exports = function (options) {
module.exports = function (source, options) {
var opts = util._extend({

@@ -14,7 +14,7 @@ short: false,

code.push(util.format('curl %s %s', opts.short ? '-X' : '--request', this.source.method));
code.push(util.format('curl %s %s', opts.short ? '-X' : '--request', source.method));
code.push(util.format('%s"%s"', opts.short ? '' : '--url ', this.source.fullUrl));
code.push(util.format('%s"%s"', opts.short ? '' : '--url ', source.fullUrl));
if (this.source.httpVersion === 'HTTP/1.0') {
if (source.httpVersion === 'HTTP/1.0') {
code.push(opts.short ? '-0' : '--http1.0');

@@ -24,23 +24,18 @@ }

// construct headers
this.source.headers.map(function (header) {
code.push(util.format('%s "%s: %s"', opts.short ? '-H' : '--header', header.name, header.value));
Object.keys(source.headersObj).sort().map(function (key) {
code.push(util.format('%s "%s: %s"', opts.short ? '-H' : '--header', key, source.headersObj[key]));
});
// construct cookies
var cookies = this.source.cookies.map(function (cookie) {
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
});
if (cookies.length) {
code.push(util.format('%s "%s"', opts.short ? '-b' : '--cookie', cookies.join('; ')));
if (source.allHeaders.cookie) {
code.push(util.format('%s "%s"', opts.short ? '-b' : '--cookie', source.allHeaders.cookie));
}
// request body
if (this.source.postData.text) {
code.push(util.format('%s %s', opts.short ? '-d' : '--data', JSON.stringify(this.source.postData.text)));
if (source.postData.text) {
code.push(util.format('%s %s', opts.short ? '-d' : '--data', JSON.stringify(source.postData.text)));
}
// construct post params
if (!this.source.postData.text && this.source.postData.params) {
this.source.postData.params.map(function (param) {
if (!source.postData.text && source.postData.params) {
source.postData.params.map(function (param) {
code.push(util.format('%s "%s=%s"', opts.short ? '-F' : '--form', param.name, param.value));

@@ -47,0 +42,0 @@ });

@@ -5,3 +5,3 @@ 'use strict';

module.exports = function (options) {
module.exports = function (source, options) {
var opts = util._extend({

@@ -25,4 +25,4 @@ queryParams: false,

// start with body pipe
if (this.source.postData && this.source.postData.text) {
code.push(util.format('echo %s | ', JSON.stringify(this.source.postData.text)));
if (source.postData && source.postData.text) {
code.push(util.format('echo %s | ', JSON.stringify(source.postData.text)));
}

@@ -68,12 +68,10 @@

code.push(util.format('http %s%s %s', flags.length ? flags.join(' ') + ' ' : '', this.source.method, opts.queryParams ? this.source.url : this.source.fullUrl));
code.push(util.format('http %s%s %s', flags.length ? flags.join(' ') + ' ' : '', source.method, opts.queryParams ? source.url : source.fullUrl));
var self = this;
// construct query params
if (opts.queryParams) {
var queryStringKeys = Object.keys(this.source.queryString);
var queryStringKeys = Object.keys(source.queryObj);
queryStringKeys.map(function (name) {
var value = self.source.queryString[name];
var value = source.queryObj[name];

@@ -91,18 +89,9 @@ if (util.isArray(value)) {

// construct headers
this.source.headers.map(function (header) {
code.push(util.format('%s:%s', header.name, header.value));
Object.keys(source.allHeaders).sort().map(function (key) {
code.push(util.format('%s:%s', key, source.allHeaders[key]));
});
// construct cookies argument
var cookies = this.source.cookies.map(function (cookie) {
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
});
if (cookies.length) {
code.push(util.format('Cookie:%s', cookies.join('; ')));
}
// construct post params
if (!this.source.postData.text && this.source.postData.params && this.source.postData.params.length) {
this.source.postData.params.map(function (param) {
if (!source.postData.text && source.postData.params && source.postData.params.length) {
source.postData.params.map(function (param) {
code.push(util.format('%s:%s', param.name, param.value));

@@ -109,0 +98,0 @@ });

@@ -5,5 +5,5 @@ 'use strict';

key: 'node',
title: 'Node.JS',
title: 'Node.js',
extname: '.js',
default: 'native'
};
'use strict';
var util = require('util');
var reducer = require('../../reducer');
module.exports = function (options) {
module.exports = function (source, options) {
var opts = util._extend({

@@ -14,21 +13,12 @@ indent: ' '

var reqOpts = {
method: this.source.method,
hostname: this.source.uriObj.hostname,
port: this.source.uriObj.port,
path: this.source.uriObj.path,
headers: this.source.headersObj
method: source.method,
hostname: source.uriObj.hostname,
port: source.uriObj.port,
path: source.uriObj.path,
headers: source.allHeaders
};
// construct cookies argument
var cookies = this.source.cookies.map(function (cookie) {
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
});
if (cookies.length) {
reqOpts.headers.Cookie = cookies.join('; ');
}
code.push('var http = require("http");');
if (!this.source.postData.text && this.source.postData.params) {
if (!source.postData.text && source.postData.params) {
code.push('var querystring = require("querystring");');

@@ -63,22 +53,6 @@ }

if (this.source.postData.text) {
code.push(util.format('req.write(%s);', JSON.stringify(this.source.postData.text)));
if (source.postData.text) {
code.push(util.format('req.write(%s);', JSON.stringify(source.postData.text)));
}
// if (!this.source.postData.text && this.source.postData.params) {
// if (this.source.postData.mimeType === 'application/x-www-form-urlencoded') {
// var postData = this.source.postData.params.reduce(reducer, {});
// code.push(util.format('var postData = querystring.stringify(%s);', JSON.stringify(postData)));
// code.push(util.format('req.write(postData);'));
// }
// if (this.source.postData.mimeType === 'multipart/form-data') {
// var postData = this.source.postData.params.reduce(reducer, {});
// code.push(util.format('var postData = querystring.stringify(%s);', JSON.stringify(postData)));
// code.push(util.format('req.write(postData);'));
// }
// }
code.push('req.end();');

@@ -85,0 +59,0 @@

@@ -6,3 +6,3 @@ 'use strict';

module.exports = function (options) {
module.exports = function (source, options) {
var opts = util._extend({

@@ -12,27 +12,28 @@ indent: ' '

var includeFS = false;
var code = ['var request = require("request");', null];
var reqOpts = {
method: this.source.method,
url: this.source.url
method: source.method,
url: source.url
};
if (Object.keys(this.source.queryObj).length) {
reqOpts.qs = this.source.queryObj;
if (Object.keys(source.queryObj).length) {
reqOpts.qs = source.queryObj;
}
if (Object.keys(this.source.headersObj).length) {
reqOpts.headers = this.source.headersObj;
if (Object.keys(source.headersObj).length) {
reqOpts.headers = source.headersObj;
}
var includeFS = false;
switch (this.source.postData.mimeType) {
switch (source.postData.mimeType) {
case 'application/x-www-form-urlencoded':
reqOpts.form = this.source.postData.paramsObj;
reqOpts.form = source.postData.paramsObj;
break;
case 'application/json':
reqOpts.body = this.source.postData.jsonObj;
reqOpts.json = true;
if (source.postData.jsonObj) {
reqOpts.body = source.postData.jsonObj;
reqOpts.json = true;
}
break;

@@ -43,3 +44,3 @@

this.source.postData.params.forEach(function (param) {
source.postData.params.forEach(function (param) {
var attachement = {};

@@ -68,7 +69,9 @@

default:
reqOpts.body = this.source.postData.text;
if (source.postData.text !== '') {
reqOpts.body = source.postData.text;
}
}
// construct cookies argument
if (this.source.cookies.length) {
if (source.cookies.length) {
reqOpts.jar = 'JAR';

@@ -79,5 +82,5 @@

var url = this.source.url;
var url = source.url;
this.source.cookies.map(function (cookie) {
source.cookies.map(function (cookie) {
code.push(util.format('jar.setCookie(request.cookie("%s=%s"), "%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), url));

@@ -88,12 +91,7 @@ });

if (includeFS) {
code.push('var fs = require("fs");');
code.unshift('var fs = require("fs");');
}
var content = JSON.stringify(reqOpts, null, opts.indent)
.replace('"JAR"', 'jar')
.replace(/"fs\.createReadStream\(\\\"(.+)\\\"\)\"/, 'fs.createReadStream("$1")');
code.push(util.format('request(%s, %s', content, 'function (error, response, body) {'));
code.push(util.format('request(%s, %s', JSON.stringify(reqOpts, null, opts.indent), 'function (error, response, body) {'));
code.push(opts.indent + 'if (error) throw new Error(error);');

@@ -105,3 +103,3 @@ code.push(null);

return code.join('\n');
return code.join('\n').replace('"JAR"', 'jar').replace(/"fs\.createReadStream\(\\\"(.+)\\\"\)\"/, 'fs.createReadStream("$1")');
};

@@ -108,0 +106,0 @@

@@ -5,3 +5,3 @@ 'use strict';

module.exports = function (options) {
module.exports = function (source, options) {
var opts = util._extend({

@@ -19,26 +19,18 @@ timeout: '10'

// Create request object
code.push('NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"' + this.source.fullUrl + '"]');
code.push('NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"' + source.fullUrl + '"]');
code.push(' cachePolicy:NSURLRequestUseProtocolCachePolicy');
code.push(' timeoutInterval:' + parseInt(opts.timeout, 10).toFixed(1) + '];');
code.push('[request setHTTPMethod:@"' + this.source.method + '"];');
code.push('[request setHTTPMethod:@"' + source.method + '"];');
// Set headers
this.source.headers.forEach(function (header) {
code.push('[request setValue:@"' + header.value + '" forHTTPHeaderField:@"' + header.name + '"];');
Object.keys(source.allHeaders).sort().map(function (key) {
code.push('[request setValue:@"' + source.allHeaders[key] + '" forHTTPHeaderField:@"' + key + '"];');
});
var cookies = this.source.cookies.map(function (cookie) {
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
});
if (cookies.length) {
code.push('[request setValue:@"' + cookies.join('; ') + '" forHTTPHeaderField:@"Cookie"];');
}
// Set request body
if (this.source.postData && (this.source.postData.params || this.source.postData.text)) {
if (source.postData && (source.postData.params || source.postData.text)) {
code.push(null);
if (this.source.postData.mimeType === 'application/x-www-form-urlencoded' && this.source.postData.params) {
var params = this.source.postData.params;
if (source.postData.mimeType === 'application/x-www-form-urlencoded' && source.postData.params) {
var params = source.postData.params;
code.push('NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"' + params[0].name + '=' + params[0].value + '" dataUsingEncoding:NSUTF8StringEncoding]];');

@@ -48,6 +40,6 @@ for (var i = 1, len = params.length; i < len; i++) {

}
} else if (this.source.postData.mimeType === 'application/json' && this.source.postData.text) {
code.push('NSData *postData = [[NSData alloc] initWithData:[@' + JSON.stringify(this.source.postData.text) + ' dataUsingEncoding:NSUTF8StringEncoding]];');
} else if (this.source.postData.text) {
code.push('NSData *postData = [[NSData alloc] initWithData:[@"' + this.source.postData.text + '" dataUsingEncoding:NSUTF8StringEncoding]];');
} else if (source.postData.mimeType === 'application/json' && source.postData.text) {
code.push('NSData *postData = [[NSData alloc] initWithData:[@' + JSON.stringify(source.postData.text) + ' dataUsingEncoding:NSUTF8StringEncoding]];');
} else if (source.postData.text) {
code.push('NSData *postData = [[NSData alloc] initWithData:[@"' + source.postData.text + '" dataUsingEncoding:NSUTF8StringEncoding]];');
}

@@ -54,0 +46,0 @@

@@ -5,3 +5,3 @@ 'use strict';

module.exports = function (options) {
module.exports = function (source, options) {
var opts = util._extend({

@@ -18,25 +18,14 @@ indent: ' '

// Create URI
code.push(util.format('let uri = Uri.of_string "%s" in', this.source.fullUrl));
code.push(util.format('let uri = Uri.of_string "%s" in', source.fullUrl));
// Add headers, including the cookies
var headersPresent = this.source.headers && this.source.headers.length;
var cookiesPresent = this.source.cookies && this.source.cookies.length;
var headers = Object.keys(source.allHeaders);
if (headersPresent || cookiesPresent) {
if (headers.length) {
code.push('let headers = Header.init ()');
if (headersPresent) {
for (var header in this.source.headers) {
code.push(util.format(opts.indent + '|> fun h -> Header.add h "%s" "%s"', this.source.headers[header].name, this.source.headers[header].value));
}
}
headers.map(function (key) {
code.push(util.format(opts.indent + '|> fun h -> Header.add h "%s" "%s"', key, source.allHeaders[key]));
});
if (cookiesPresent) {
var cookie = this.source.cookies.map(function (cookie) {
return cookie.name + '=' + cookie.value;
}).join('; ');
code.push(util.format(opts.indent + '|> fun h -> Header.add h "Cookie" "%s"', cookie));
}
code.push('in');

@@ -46,16 +35,5 @@ }

// Add body
var bodyPresent = this.source.postData && this.source.postData.text;
if (bodyPresent) {
if (source.postData.text) {
// Just text
code.push(util.format('let body = %s in', JSON.stringify(this.source.postData.text)));
} else if (this.source.postData && !this.source.postData.text && this.source.postData.params && this.source.postData.params.length) {
// Post params
bodyPresent = true;
var body = this.source.postData.params.map(function (param) {
return param.name + '=' + param.value;
}).join('&');
code.push(util.format('let body = "%s" in', body));
code.push(util.format('let body = %s in', JSON.stringify(source.postData.text)));
}

@@ -67,5 +45,5 @@

code.push(util.format('Client.call %s%s(Code.method_of_string "%s") uri',
(headersPresent || cookiesPresent) ? '~headers ' : '',
bodyPresent ? '~body ' : '',
this.source.method
headers.length ? '~headers ' : '',
source.postData.text ? '~body ' : '',
source.method
));

@@ -82,5 +60,5 @@

key: 'cohttp',
title: 'OCaml',
title: 'CoHTTP',
link: 'https://github.com/mirage/ocaml-cohttp',
description: 'Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml'
};

@@ -5,3 +5,3 @@ 'use strict';

module.exports = function (options) {
module.exports = function (source, options) {
var opts = util._extend({

@@ -28,7 +28,7 @@ indent: ' ',

name: 'CURLOPT_PORT',
value: this.source.uriObj.port
value: source.uriObj.port
}, {
escape: true,
name: 'CURLOPT_URL',
value: this.source.fullUrl
value: source.fullUrl
}, {

@@ -53,11 +53,11 @@ escape: false,

name: 'CURLOPT_HTTP_VERSION',
value: this.source.httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1'
value: source.httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1'
}, {
escape: true,
name: 'CURLOPT_CUSTOMREQUEST',
value: this.source.method
value: source.method
}, {
escape: true,
name: 'CURLOPT_POSTFIELDS',
value: this.source.postData ? this.source.postData.text : undefined
value: source.postData ? source.postData.text : undefined
}];

@@ -76,3 +76,3 @@

// construct cookies
var cookies = this.source.cookies.map(function (cookie) {
var cookies = source.cookies.map(function (cookie) {
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);

@@ -86,4 +86,4 @@ });

// construct cookies
var headers = this.source.headers.map(function (header) {
return util.format('"%s: %s"', header.name, header.value);
var headers = Object.keys(source.headersObj).sort().map(function (key) {
return util.format('"%s: %s"', key, source.headersObj[key]);
});

@@ -90,0 +90,0 @@

@@ -5,3 +5,3 @@ 'use strict';

module.exports = function (options) {
module.exports = function (source, options) {
var opts = util._extend({

@@ -21,34 +21,15 @@ indent: ' ',

code.push(util.format('--method %s', this.source.method));
code.push(util.format('--method %s', source.method));
// construct cookies argument
var cookies = this.source.cookies.map(function (cookie) {
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
Object.keys(source.allHeaders).map(function (key) {
code.push(util.format('--header "%s: %s"', key, source.allHeaders[key]));
});
if (cookies.length) {
code.push(util.format('--header "Cookie: %s"', cookies.join('; ')));
if (source.postData.text) {
code.push('--body-data ' + JSON.stringify(source.postData.text));
}
this.source.headers.map(function (header) {
code.push(util.format('--header "%s: %s"', header.name, header.value));
});
if (this.source.postData.text) {
code.push('--body-data ' + JSON.stringify(this.source.postData.text));
}
if (this.source.postData.mimeType === 'multipart/form-data') {
this.source.postData.params.forEach(function (param) {
if (param.value) {
code.push(util.format('--body-data %s', JSON.stringify(param.value)));
} else if (param.fileName) {
code.push(util.format('--body-file "%s"', param.fileName));
}
});
}
code.push(opts.short ? '-O' : '--output-document');
code.push(util.format('- "%s"', this.source.fullUrl));
code.push(util.format('- "%s"', source.fullUrl));

@@ -55,0 +36,0 @@ return code.join(opts.indent !== false ? ' \\\n' + opts.indent : ' ');

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc