Burak
Burak

Reputation: 5774

I am getting 400 BAD REQUEST in tastypie

I am using tastypie. When I want to post data to my server I am getting:

My curl command is:

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{{"shop" : "/api/shop/1/","transactions" : [{"item" : "/api/item/53/","note" : "Normal"}]}' 'http://localhost:5000/api/order/'

The response is:

HTTP/1.0 400 BAD REQUEST
Date: Mon, 21 Jul 2014 13:41:52 GMT
Server: WSGIServer/0.1 Python/2.7.5
Access-Control-Allow-Headers: Origin,Content-Type,Accept
Content-Language: tr
Vary: Accept-Language, Cookie
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETE
Content-Type: text/html; charset=utf

Table create syntaxes:

CREATE TABLE `t_order` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `table` varchar(100) NOT NULL,
  `shop_id` int(11) NOT NULL,
  `date_modified` datetime NOT NULL,
  `status` int(11) DEFAULT '0',
  `date_created` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `shop_id_refs_id_d3748fa9` (`shop_id`),
  CONSTRAINT `shop_id_refs_id_d3748fa9` FOREIGN KEY (`shop_id`) REFERENCES `t_shop` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=231 DEFAULT CHARSET=utf8;

CREATE TABLE `t_transaction` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `order_id` int(11) NOT NULL,
  `note` varchar(500) NOT NULL,
  `item_id` int(11) NOT NULL,
  `closed` tinyint(1) NOT NULL,
  `date_modified` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `item_id_refs_id_348f1ee9` (`item_id`),
  KEY `order_id_refs_id_d9a0bcc6` (`order_id`),
  CONSTRAINT `item_id_refs_id_348f1ee9` FOREIGN KEY (`item_id`) REFERENCES `t_item` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=71 DEFAULT CHARSET=utf8;

My models:

class Order(models.Model):
STATUSES = (
    (0, 'OPEN'),
    (1, 'CLOSED'),
    (2, 'CANCELLED'),
)
table = CharField(max_length=100)
shop = ForeignKey(Shop)
date_modified = DateTimeField(auto_now=True)
date_created = DateTimeField(auto_now_add=True)
status = IntegerField(choices=STATUSES)
class Meta:
        db_table = 't_order'

class Transaction(models.Model):
order = ForeignKey('Order')
note = CharField(max_length=500)
item = ForeignKey(Item)
closed = BooleanField(default=False)
date_modified = DateTimeField(auto_now=True)

class Meta:
        db_table = 't_transaction'

What is the problem?

Upvotes: 0

Views: 1467

Answers (1)

Bartosz Dabrowski
Bartosz Dabrowski

Reputation: 1860

Your JSON is invalid. Please validate your curl data here. The 400 (bad request) status should give you clue about that. To make sure you can create serializer which can handle that exception.

from tastypie.exceptions import BadRequest

class VerboseSerializer(Serializer):
    """
    Gives message when loading JSON fails.
    """
    # Tastypie>=0.9.6,<=0.11.0
    def from_json(self, content):
        """
        Override method of `Serializer.from_json`. Adds exception message when loading JSON fails.
        """
        try:
            return json.loads(content)
        except ValueError as e:
            raise BadRequest(u"Incorrect JSON format: Reason: \"{}\" (See www.json.org for more info.)".format(e.message))

class MyResource(BaseModelResource):
    class Meta:
        serializer = VerboseSerializer(formats=['json'])

Upvotes: 1

Related Questions