Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

generating a configured Request?

26 views
Skip to first unread message

Jonathan Vanasco

unread,
Mar 25, 2025, 2:27:52 PMMar 25
to pylons-discuss
I can't seem to generate a request properly.

For example, in `main(`:

from pyramid.interfaces import IRequestFactory
from pyramid.request import Request

app = config.make_wsgi_app()
request_factory = app.registry.queryUtility(IRequestFactory, default=Request)
request = request_factory.blank("/")

The query for IRequestFactory always returns None,  leaving me with the default `Request` (which has none of the configured `add_request_method` attributes)

I've also tried this using `get_app(config_uri)` from scripting. 

Am I querying the wrong interface, or is this just not a compatible use-case?


Michael Merickel

unread,
Mar 25, 2025, 3:37:58 PMMar 25
to pylons-...@googlegroups.com
The request factory doesn't fully do every step of initializing a request since it's a user-overridable hook. It's not really intended to be used directly. To have a fully initialize request you later need to do a couple steps iirc.

1. request.registry = app.registry
2. pyramid.request.apply_request_extensions(request)

This is off the top of my head so I may be forgetting a step, apologies but hopefully this is helpful.

The "right" way to do this is to use pyramid.scripting.prepare:

with pyramid.scripting.prepare(registry=app.registry) as env:
    request = env['request']

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/pylons-discuss/c46029eb-e0c4-4f75-82d1-2971f55a49bcn%40googlegroups.com.


--

Michael

Jonathan Vanasco

unread,
Mar 25, 2025, 3:53:01 PMMar 25
to pylons-discuss
> The "right" way to do this is to use pyramid.scripting.prepare:

That's even easier for me to implement.  Thank you so much, Michael!

Jonathan Vanasco

unread,
Mar 25, 2025, 4:49:07 PMMar 25
to pylons-discuss
Michael- one more question:

Do you know if there is better way to stuff POST/GET vars in this context than the following?

> request.environ["REQUEST_METHOD"] = "POST"
> wsgi_input = BytesIO(b"")
> request.body_file_raw = wsgi_input
> request.environ["webob._parsed_post_vars"] = (options, wsgi_input)

This is leveraging (abusing) the `webob._parsed_post_vars` storage and a check against `.body_file_raw`


On Tuesday, March 25, 2025 at 3:37:58 PM UTC-4 Michael Merickel wrote:

Theron Luhn

unread,
Mar 25, 2025, 6:16:08 PMMar 25
to pylons-...@googlegroups.com
Something like this should work:

request = Request.blank(“/“, POST={“foo”: “bar”})
with pyramid.scripting.prepare(registry=app.registry, request=request):
    ...

— Theron



Jonathan Vanasco

unread,
Mar 25, 2025, 8:05:16 PMMar 25
to pylons-discuss
> Something like this should work:

Wonderful!  Thank you.

I am hoping to replace all mocked requests in tests and some commandline utilities.  
Reply all
Reply to author
Forward
0 new messages