openapi-to-postman icon indicating copy to clipboard operation
openapi-to-postman copied to clipboard

How to add or convert a Collection Pre-Request Script?

Open skplunkerin opened this issue 5 years ago • 5 comments

How can I include a Collection Pre-Request Script?

I have a Collection Pre-Request Script that will setup a header for all requests made by my collection, but I can't find how to add this script into OpenAPI format to be converted correctly? OR is there a way the package can insert this script instead of adding it in OpenAPI?

Here's the script I'm wanting to include after running openapi-to-postmanv2:

pm.request.headers.add({key: 'X-Project-Client', value: "{{X-Project-Client}}" })

When I manually add this to my collection and export it, it will appear like this in the export json (edited for brevity):

{
	"info": {
		"_postman_id": "12345",
		"name": "Collection Name",
		"description": "My collection description",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
	},
	"item": [...]
	"event": [
		{
			"listen": "prerequest",
			"script": {
				"id": "12234",
				"type": "text/javascript",
				"exec": [
					"pm.request.headers.add({key: 'X-Project-Client', value: \"{{X-Project-Client}}\" })"
				]
			}
		},
		{
			"listen": "test",
			"script": {
				"id": "1234",
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		}
	],
	"variable": [...],
}

skplunkerin avatar Jan 22 '20 23:01 skplunkerin

@skplunkerin Is there a similar workflow in the other tools that you use that supports something similar?

We could explore Postman vendor-extensions in OAS that allow you to specify a X-Postman-PreRequestScript in your OpenAPI document, that's included as a pre-request script in the generated collection.

abhijitkane avatar Apr 27 '20 10:04 abhijitkane

@abhijitkane I was actually thinking the same thing about using OAS vendor extensions for this, and other Postman specific things.

Right now I'm currently breaking OAS validation by adding a non-supported component (not related to PreRequest Scripts) that I need for Postman, but OAS doesn't recognize. It would be really nice to have multiple Postman-specific features that I can add directly to my OAS file using vendor extensions, so both OAS and Postman are happy.

skplunkerin avatar Apr 27 '20 13:04 skplunkerin

I agree - it would be nice to automate certain post api collection generation steps via a custom oas3 definition

  • add pre-request scripts
  • define tests
  • prefill Params / Headers / Body values with environment variables
  • apply custom endpoint settings

Right now it's pretty burdensome to reapply all custom Postman features to an oas3-generated collection on each release

kepikoi avatar Feb 01 '21 13:02 kepikoi

The openapi-to-postman package focusses on transforming a OpenAPI document to a Postman collection.

To make the transformation more dynamic, we have created a seperate package Portman that uses "openapi-to-postman" under the hood to transform OpenAPI document to a Postman collection, but while doing so, it offers options to generate automat tests, insert postman variables, prefill Params / Headers / Body values with environment variables, add pre-request scripts, ...

We initially created a PR (#225) on "openapi-to-postman", but it didnt feel right to mix functionality and that is why we created the Portman package, next to openapi-to-postman, leveraging openapi-to-postman but with automation, API testing & making Postman more dynamic in mind.

thim81 avatar Jul 24 '21 15:07 thim81

Wow, I just sat down to finish a PR for this feature. I'll be checking out portman, thanks @thim81!

scottpopplewell avatar Jul 24 '21 17:07 scottpopplewell