Skip to content

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

@skplunkerin

Description

@skplunkerin

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": [...],
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature request, accepted for probable development

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions