diff --git a/CHANGELOG.md b/CHANGELOG.md index ee9afb1ae..f3dfd98ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## [Unreleased] +## [v1.14.0] - 2024-10-10 + +### Fixed + +- Fix for - [#5330](https://github.com/postmanlabs/postman-app-support/issues/5330) Added support for usage of --data-binary flag when using long format option for body type binary. + +- Fix for - [#540](https://github.com/postmanlabs/postman-code-generators/issues/540) Curl codesnippet's JSON body must follow multiLine option's configuration. + ## [v1.13.0] - 2024-09-11 ### Fixed @@ -176,7 +184,9 @@ v1.0.0 (May 29, 2020) - Add ES6 syntax support for NodeJS Request, NodeJS Native and NodeJS Unirest - Fix snippet generation for powershell and jquery, where form data params had no type field -[Unreleased]: https://github.com/postmanlabs/postman-code-generators/compare/v1.13.0...HEAD +[Unreleased]: https://github.com/postmanlabs/postman-code-generators/compare/v1.14.0...HEAD + +[v1.14.0]: https://github.com/postmanlabs/postman-code-generators/compare/v1.13.0...v1.14.0 [v1.13.0]: https://github.com/postmanlabs/postman-code-generators/compare/v1.12.0...v1.13.0 diff --git a/README.md b/README.md index 607cf4166..18684d8c0 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ _Manage all of your organization's APIs in Postman, with the industry's most com *Supercharge your API workflow.* *Modern software is built on APIs. Postman helps you develop APIs faster.* -# postman-code-generators [![Build Status](https://travis-ci.com/postmanlabs/postman-code-generators.svg?branch=master)](https://travis-ci.com/postmanlabs/postman-code-generators) +# postman-code-generators This module converts a [Postman SDK](https://github.com/postmanlabs/postman-collection) Request [Object](https://www.postmanlabs.com/postman-collection/Request.html) into a code snippet of chosen language. @@ -52,7 +52,7 @@ List of supported code generators: | Swift | URLSession | ## Table of contents -- [postman-code-generators ![Build Status](https://travis-ci.com/postmanlabs/postman-code-generators)](#postman-code-generators-) +- [postman-code-generators - [Table of contents](#table-of-contents) - [Getting Started](#getting-started) - [Prerequisite](#prerequisite) diff --git a/codegens/curl/lib/index.js b/codegens/curl/lib/index.js index 8abc73df6..af57ca4d3 100644 --- a/codegens/curl/lib/index.js +++ b/codegens/curl/lib/index.js @@ -151,9 +151,20 @@ self = module.exports = { let rawBody = body.raw.toString(), isAsperandPresent = _.includes(rawBody, '@'), // Use the long option if `@` is present in the request body otherwise follow user setting - optionName = isAsperandPresent ? '--data-raw' : form('-d', format); - // eslint-disable-next-line max-len - snippet += indent + `${optionName} ${quoteType}${sanitize(rawBody, trim, quoteType)}${quoteType}`; + optionName = isAsperandPresent ? '--data-raw' : form('-d', format), + sanitizedBody = sanitize(rawBody, trim, quoteType); + + if (!multiLine) { + try { + sanitizedBody = JSON.stringify(JSON.parse(sanitizedBody)); + } + catch (e) { + // Do nothing + } + } + + snippet += indent + `${optionName} ${quoteType}${sanitizedBody}${quoteType}`; + break; } @@ -201,7 +212,7 @@ self = module.exports = { }); break; case 'file': - snippet += indent + form('-d', format); + snippet += indent + (format ? '--data-binary' : '-d'); snippet += ` ${quoteType}@${sanitize(body[body.mode].src, trim)}${quoteType}`; break; default: diff --git a/codegens/curl/test/unit/convert.test.js b/codegens/curl/test/unit/convert.test.js index 849395a5e..d3c4c5061 100644 --- a/codegens/curl/test/unit/convert.test.js +++ b/codegens/curl/test/unit/convert.test.js @@ -289,6 +289,50 @@ describe('curl convert function', function () { }); }); + it('should return snippet with JSON body in single line if multiline option is false', function () { + request = new Request({ + 'method': 'POST', + 'header': [], + 'body': { + 'mode': 'raw', + 'raw': '{\n "name": "John",\n "type": "names",\n "id": "123sdaw"\n}', + 'options': { + 'raw': { + 'language': 'json' + } + } + }, + 'url': { + 'raw': 'https://postman-echo.com/post', + 'protocol': 'https', + 'host': [ + 'postman-echo', + 'com' + ], + 'path': [ + 'post' + ] + } + }); + options = { + multiLine: false, + longFormat: false, + lineContinuationCharacter: '\\', + quoteType: 'single', + requestTimeoutInSeconds: 0, + followRedirect: true, + followOriginalHttpMethod: false + }; + + convert(request, options, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + expect(snippet).to.be.a('string'); + expect(snippet).to.contain('-d \'{"name":"John","type":"names","id":"123sdaw"}\''); + }); + }); + it('should return snippet with backslash(\\) character as line continuation ' + 'character for multiline code generation', function () { request = new Request({ @@ -1144,5 +1188,37 @@ describe('curl convert function', function () { }); }); }); + + it('should use --data-binary when request body type is binary', function () { + var request = new Request({ + 'method': 'POST', + 'header': [], + 'body': { + 'mode': 'file', + 'file': { + 'src': 'file-path/collection123.json' + } + }, + 'url': { + 'raw': 'https://postman-echo.com/get', + 'protocol': 'https', + 'host': [ + 'postman-echo', + 'com' + ], + 'path': [ + 'get' + ] + } + }); + + convert(request, { longFormat: true }, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + expect(snippet).to.be.a('string'); + expect(snippet).to.include('--data-binary \'@file-path/collection123.json\''); + }); + }); }); }); diff --git a/codegens/python-http.client/lib/python-httpclient.js b/codegens/python-http.client/lib/python-httpclient.js index 498367663..f33886b85 100644 --- a/codegens/python-http.client/lib/python-httpclient.js +++ b/codegens/python-http.client/lib/python-httpclient.js @@ -140,7 +140,12 @@ self = module.exports = { snippet += 'from codecs import encode\n'; } snippet += '\n'; - snippet += `conn = http.client.HTTPSConnection("${sanitize(host)}"`; + if (request.url.protocol === 'http') { + snippet += `conn = http.client.HTTPConnection("${sanitize(host)}"`; + } + else { + snippet += `conn = http.client.HTTPSConnection("${sanitize(host)}"`; + } snippet += url.port ? `, ${request.url.port}` : ''; snippet += options.requestTimeout !== 0 ? `, timeout = ${options.requestTimeout})\n` : ')\n'; diff --git a/codegens/python-http.client/test/unit/converter.test.js b/codegens/python-http.client/test/unit/converter.test.js index a60da8a1d..b409c31e6 100644 --- a/codegens/python-http.client/test/unit/converter.test.js +++ b/codegens/python-http.client/test/unit/converter.test.js @@ -336,6 +336,29 @@ describe('Python-http.client converter', function () { }); }); + it('should generate valid snippets when url uses http protocol', function () { + var request = new Request({ + 'method': 'GET', + 'header': [], + 'url': { + 'raw': 'http://localhost:3000', + 'protocol': 'http', + 'host': [ + 'localhost' + ], + 'port': '3000' + }, + 'response': [] + }); + convert(request, {}, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + expect(snippet).to.be.a('string'); + expect(snippet).to.include('conn = http.client.HTTPConnection("localhost", 3000)'); + }); + }); + }); describe('parseBody function', function () { diff --git a/package-lock.json b/package-lock.json index c5c0623c9..d3540ab45 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "postman-code-generators", - "version": "1.13.0", + "version": "1.14.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index cb23c862a..1bf813ba1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postman-code-generators", - "version": "1.13.0", + "version": "1.14.0", "description": "Generates code snippets for a postman collection", "main": "index.js", "directories": {