Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python (tastypie) - Bad request on POST, returns "error" dict

I'm trying to send data to my Django-powered server using Tastypie.

I have this model

class Open(models.Model):
    name=models.TextField()

and this URLconf

open_resource=OpenResource()

urlpatterns = patterns('',
    url(r'^api/', include(open_resource.urls)),
    url(r'^admin/', include(admin.site.urls)),
)

when I run the tastypie curl command

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"name","awdawd"}' http://localhost:8000/api/open/

I get the error

HTTP/1.0 400 BAD REQUEST
Date: Sat, 05 Apr 2014 12:18:48 GMT
Server: WSGIServer/0.1 Python/2.7.3
X-Frame-Options: SAMEORIGIN
Content-Type: application/json

{"error": ""}

I've tried everything and can't seem to get this working.

Does anyone have a clue on this one?

Thanks a lot in advance

like image 214
user3264316 Avatar asked Jul 02 '26 20:07

user3264316


1 Answers

I get this unhelpful error whenever I provide invalid JSON data.

Correct JSON format is:

{"foo": "bar"}                     // correct!

[{"foo": "bar"}, {"fiz": "baz"}]   // correct!

{"foo": "bar", "fiz": "baz"}       // correct!

Examples of common mistakes:

{'foo': 'bar'}    // error is using single quotes instead of double quotes
{foo: "bar"}      // error is not using double quotes for "foo"
{"foo", "bar"}    // error is using a comma (,) instead of a colon (:)  ← Your error

Examples of more complex mistakes:

[{"foo": "bar"}, {"fiz": "baz"},]
// error is using a comma at the end of a list or array

{"foo": "bar", "fiz": "baz",}  // courtesy @gthmb
// error is using a comma at the end of the final key-value pair

Think your JSON is valid? Double-check with a JSON validator.

like image 109
Nick Merrill Avatar answered Jul 04 '26 08:07

Nick Merrill