Pre-request Execution from external call

Does the pre-request script execute if we don’t directly hit “Send” (ie. trigger the request) via Postman?

Because I have a mock server set up with a simple POST request. In the pre-request script, I change the url for the request by doing: pm.request.url.update(“new_url”). This works as expected when I hit “Send” for the request in Postman, but the script does not seem to get triggered when I try to hit this endpoint from outside Postman?

Is this on purpose? I thought the pre-request scripts executed whenever the endpoint was hit?

What do you mean by outside of Postman? and what endpoint are we talking about (the URL for the mock?)

If you are hitting the URL for the mock directly, then its only going to return a JSON body from the example.

That’s not going to trigger any code in postman which is contained in a collection.

The pre-request scripts can only be executed in the Postman app (web or desktop) or via the Newman command line tool (and I guess the new CLI tool).

The mock and collection are essentially separate.
You could have multiple mocks within the same work space and call them from different collections. The mocks are contained in the work space, and the collection is another item within the workspace. The collection contains the requests which can include code.

The mock itself does not know about the code.

The mock may be generated from a request in a collection, but its not actually linked to the collection per se.

1 Like

Outside as in an external service (for example an AWS Lambda function) that sends a POST request to the endpoint. I’m talking about the mock server url. I have a request set up as: {{mock_url}}/endpoint.
I was under the assumption that the pre-request script would run whenever this endpoint was hit from anywhere. Is that not the case?

I don’t understand why they would not have this set up? What’s the point of having a mock server if you can’t do pre/post-modifications from external services hitting the endpoints.

The pre-request scripts are not linked to the mock server\service.

The pre-request scripts are linked to the requests in the collection.

You have to execute the request for the pre-request scripts to trigger.

If you are hitting the mock server directly, then it has no awareness of the collection details, the requests themselves or the pre-request scripts.

When you create a mock, it gets added to the workspace, not the collection.

I can’t answer in relation to the usefulness of the mocks, as I don’t use it a lot.

Isn’t the point thought to create examples of the response types and then generate a mock from those examples. You can add extra headers to specifically target a particular example.

The Postman request (that includes the pre-request script) is consuming the mock.

Your Lambda function is consuming the mock, so if you want to change headers, the URL, or something in the request, then you need to do that in your function.

Surely if you change the URL, then you won’t be hitting the mock at all?

1 Like

I just reviewed the Mock instructions again.

It does actually link the mock to the examples in the collection.

If you add a new example, it becomes available in the mock.

However, the examples do not have a pre-request or tests tab, so don’t execute code, so the principle is pretty much the same as what I put in the previous comment. If you need to modify the URL or what is being sent, you need to do this in your function.

1 Like

I see. Thank you for the clarifications (and help)!