0

I need to pass filters to the api via XML and not via GET query params.

I've doing this:

curl --dump-header - \
    -H "Content-Type: application/xml" -X GET \
    --data '<object><title>Hello XML</title><date>200-01-01</date></object>' \
    http://x.x.x.x/api/entry/

which I want to be the same as: http://x.x.x.x/api/entry/?format=xml&title=Hello XML&date=200-01-01 but --data gets ignored for GET request. So, my question is, how to I pass XML to a GET request using tastypie?

Thanks in advance for the help.

EDIT

I also should note, that in the XML data I want to be able to set the limit and offset, along with filter.

imns
  • 4,996
  • 11
  • 57
  • 80
  • You're probably aware of this, but this probably isn't a good idea. :) See http://stackoverflow.com/questions/978061/http-get-with-request-body#978094 – David Eyk Sep 05 '12 at 22:53
  • Also, the problem may be more with `curl` than tastypie, if `curl` is ignoring the `--data` option. – David Eyk Sep 05 '12 at 22:55
  • David, I'm also using the python requests module to test and it also does not work. – imns Sep 06 '12 at 02:32
  • I would not recommend this as Tastypie in the end was developed for JSON interaction rather than XML. Why not just encode your data from XML to JSON and everything would be so much easier? – Alex Botev Sep 06 '12 at 12:04
  • @Belov This is wildly inaccurate--Tastypie natively supports 6 serialization formats, by default, XML being one of them. – David Eyk Sep 06 '12 at 16:06

1 Answers1

2

Your best bet is probably to override Resource.dispatch_list() to parse the filters out of the request body and bring them into the keyword arguments. Something like this:

def dispatch_list(self, request, **kwargs):
    body_filters = parse_xml_get_data(request) # <- MAGIC: returns a dict()
    kwargs.update(body_filters)
    return super(MyResource, self).dispatch_list(request, **kwargs)

When you're subverting the framework this deeply, I'd highly recommend reading through TastyPie's request-response cycle and resources.py, so you can completely understand what you're doing.

Also, for writing your parse_xml_get_data() function there, you'll need to get at the raw request body.

David Eyk
  • 12,171
  • 11
  • 63
  • 103
  • This is assuming that the GET request body ever makes it as far as Tastypie, which is beginning to sound a bit iffy. You may have to parse your XML on the client side to send as GET parameters after all. – David Eyk Sep 05 '12 at 23:05
  • So it sounds like I'm trying to make tastypie do something it wasn't really designed for. Maybe I should look at other alternatives. – imns Sep 06 '12 at 12:26
  • More like you're trying to make HTTP GET do something it wasn't really designed for. IF you can find a client and a server that are willing to work with an HTTP GET request body, then tastypie should actually handle it very gracefully. – David Eyk Sep 06 '12 at 16:08