Using test.client.Client.post with body?

776 views
Skip to first unread message

Mikeal Rogers

unread,
Oct 3, 2006, 2:41:04 PM10/3/06
to django-d...@googlegroups.com
I'm a little confused as to how to really use the test client for a
json-rpc request.

I need to send a serialized object in the body, but the method seems
to be designed to simulate form post and may not contain this option.

Is there a way to do this that I'm missing or does this need to be
added.

I think most of my confusion surrounding this is that I'm used to
using httplib to make these requests, but the client test tool seems
to make it's requests directly to the request handler apis. A note
about this in the documentation would really be helpful since I
wouldn't have guessed this without looking through the source.

-Mikeal

Russell Keith-Magee

unread,
Oct 3, 2006, 8:46:46 PM10/3/06
to django-d...@googlegroups.com
On 10/4/06, Mikeal Rogers <mik...@osafoundation.org> wrote:
>
> I'm a little confused as to how to really use the test client for a
> json-rpc request.
>
> I need to send a serialized object in the body, but the method seems
> to be designed to simulate form post and may not contain this option.

This is correct; the intention of the test client is to make it easy
to stimulate Django responses to GET and POST requests without having
to manually construct the request from scratch. Since the most common
use case is GETting a page and POSTing a form, these are the use cases
that are addressed by the test API.

> Is there a way to do this that I'm missing or does this need to be
> added.

It's entirely possible that something needs to be added. As it says in
the docs - the test framework is new and under development. So far, it
has met _my_ needs - after all, I wrote it - but if there is a use
case I have missed, I'm happy to do what I can to accommodate it.

I'm not particularly familiar with the requirements of json-rpc - from
a quick look at the website, it looks like what you need is to be able
to compose a POST request that isn't multipart.

You can do this using the extra kwargs to override the default POST dictionary:

payload = ... your payload ...
stream = StringIO(payload)
client.post(url,
CONTENT_TYPE='text/json',
CONTENT_LENGTH=len(payload),
wsgi.input = stream)

You could also use client.request() - this is essentially the same
approach, but doesn't assume any POST-like defaults.

Now - obviously this isn't the cleanest API. I'm hesitant to add a
specific 'json-rpc' interface to Client - this would lead to a
proliferation of interfaces for protocols (xml-rpc, soap,...).
However, I could be easily convinced to add a generic solution -
something like:

Client.raw_post(path, payload, payload_type='text/json')

if it will satisfy the general RPC testing use case. Alternative
suggestions are also welcome.

> I think most of my confusion surrounding this is that I'm used to
> using httplib to make these requests, but the client test tool seems
> to make it's requests directly to the request handler apis. A note
> about this in the documentation would really be helpful since I
> wouldn't have guessed this without looking through the source.

I'm not sure talking about the internals would improve the
documentation. However, it is probably worth highlighting that the
Client interface is slightly different to that of httplib. Raise an
enhancement request for clarification of the docs, and assign it to me
(russellm).

Yours,
Russ Magee %-)

Afternoon

unread,
Oct 4, 2006, 6:03:04 AM10/4/06
to Django developers
> Client.raw_post(path, payload, payload_type='text/json')

+1

I have a testing apparatus, which I built for a pre-MR project, I use
it to submit SOAP-style XML requests and all kinds of stuff. I'm
looking to port the project to MR and the tests to your test framework
at some point in the future, I would need this method to correctly
exercise all my views.

I'd be happy to help write it, if that would be useful, but I don't
think I'll be moving versions for at least a month or two.

ryan liang

unread,
Aug 26, 2014, 2:45:03 AM8/26/14
to django-d...@googlegroups.com
"""
payload = ... your payload ...
stream = StringIO(payload)
client.post(url,
    CONTENT_TYPE='text/json',
    CONTENT_LENGTH=len(payload),
    wsgi.input = stream)
"""
It does not work.


在 2006年10月4日星期三UTC+8上午8时46分46秒,Russell Keith-Magee写道:

Florian Apolloner

unread,
Aug 26, 2014, 7:28:53 AM8/26/14
to django-d...@googlegroups.com
On Tuesday, August 26, 2014 8:45:03 AM UTC+2, ryan liang wrote:
"""
payload = ... your payload ...
stream = StringIO(payload)
client.post(url,
    CONTENT_TYPE='text/json',
    CONTENT_LENGTH=len(payload),
    wsgi.input = stream)
"""
It does not work.

Obviously not, wsgi.input is not a valid kwarg after all, pass it in via data…
Reply all
Reply to author
Forward
0 new messages