Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to override post-response script content for the postman? #850

Closed
1 task done
hardiklakhalani opened this issue May 11, 2024 · 2 comments
Closed
1 task done
Labels

Comments

@hardiklakhalani
Copy link

hardiklakhalani commented May 11, 2024

Scribe version

4.35.0

Your question

How to override post-response script content for the postman?

Here is the script I want to add in my login request:

pm.collectionVariables.set("token", pm.response.json().data.token);

image

Following is the variables I've already overridden successfully:

But not sure how to add the post-response script to the login request.

'postman' => [
        'enabled' => true,
        'overrides' => [
            // 'info.version' => '2.0.0',
            'info.name' => 'Example API',
            'info.description' => 'API documentation for the Example API.',
            'variables' => [
                [
                    "key"=> "baseUrl",
                    "value"=>"http://xyz_site.test",
                    "type"=>"string"
                ],
                [

                    "key"=>"token",
                    "value"=> null,
                    "type"=>"string"
                ],
                [
                    "key"=>"password",
                    "value"=>"12345678",
                    "type"=>"string"
                ],
                [
                    "key"=>"email_user",
                    "value"=>"[email protected]",
                    "type"=>"string"
                ]
            ],
            "auth" => [
                "type" => "bearer",
                "bearer" => [
                    "key" => "token",
                    "value" => "{{token}}",
                    "type" => "string"
                ]
            ]
        ],
    ],

Following is the post-response script example snippet from the postman collection export:

{
	"item": [
		{
			"name": "Authentication",
			"item": [
				{
					"name": "Login User",
					"event": [
						{
							"listen": "test",
							"script": {
								"exec": [
									"pm.collectionVariables.set(\"token\", pm.response.json().data.token);"
								],
								"type": "text/javascript",
								"packages": {}
							}
						},
						{
							"listen": "prerequest",
							"script": {
								"exec": [
									""
								],
								"type": "text/javascript",
								"packages": {}
							}
						}
					],
					"request": {
						"auth": {
							"type": "noauth"
						},
						"method": "POST",
						"header": [
							{
								"key": "Content-Type",
								"value": "application/json"
							},
							{
								"key": "Accept",
								"value": "application/json"
							}
						],
						"body": {
							"mode": "raw",
							"raw": "{\"email\":\"{{email_admin}}\",\"password\":\"12345678\"}"
						},
						"url": {
							"raw": "{{baseUrl}}/api/login",
							"host": [
								"{{baseUrl}}"
							],
							"path": [
								"api",
								"login"
							]
						}
					},
					"response": []
				}
			]
		},
	]
}

Docs

@shalvah
Copy link
Contributor

shalvah commented May 27, 2024

I have no idea. Where is the script meant to be specified in the Postman collection file format? Doesn't the overrides section work?

@shalvah shalvah removed the triage label May 27, 2024
@ahmed-aliraqi
Copy link
Contributor

ahmed-aliraqi commented Jan 4, 2025

This works with me :

// config/scribe.php
...
'postman' => [
        'enabled' => true,

        'overrides' => [
            // 'info.version' => '2.0.0',
            'auth' => [
                'type' => 'bearer',
                'bearer' => [
                    [
                        'key' => 'token',
                        'value' => '{{accessToken}}',
                        'type' => 'string',
                    ],
                ],
            ],
            "event" => [
                [
                    "listen" => "test",
                    "script" => [
                        "type" => "text/javascript",
                        "exec" => [
                            "const paths = [",
                            "    '/api/login',",
                            "    '/api/register'",
                            "];",
                            "",
                            "if (paths.includes(pm.request.url.getPath())) {",
                            "    var response = JSON.parse(responseBody);",
                            "",
                            "    if (response.token) {",
                            "        if(pm.environment.name) {",
                            "            pm.environment.set('accessToken', response.token);",
                            "        } else {",
                            "            pm.collectionVariables.set('accessToken', response.token);",
                            "        }",
                            "    }",
                            "}"
                        ]
                    ]
                ]
            ]
        ],
    ],
    ...

@shalvah shalvah closed this as completed Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants