Python (tastypie) - POST 请求错误,返回 "error"dict

标签 python json django http tastypie

我正在尝试使用 Tastypie 将数据发送到我的 Django 驱动的服务器。

我有这个模型

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

和这个 URLconf

open_resource=OpenResource()

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

当我运行 tastypie curl 命令时

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

我得到了错误

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": ""}

我已经尝试了所有方法,但似乎无法正常工作。

有人知道这个吗?

提前致谢

最佳答案

每当我提供无效的 JSON 数据时,我都会收到这个无用的错误。

正确 JSON 格式为:

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

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

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

常见错误示例:

{'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

更复杂的错误示例:

[{"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

认为您的 JSON 有效?用 JSON validator 仔细检查.

关于Python (tastypie) - POST 请求错误,返回 "error"dict,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22880870/

相关文章:

python - 将模型添加到特定用户(Django)

python - 从 .csv 文件计算所需的浮点迭代

javascript - 如何使用字段名称创建 JSON 字符串

json - JSON中的反序列化和序列化是什么?

python - Django:Celery Worker 未启动(没有任何错误)

python - 使用 Pandas 基于正则表达式分离列数据

python - pip 在 Mac OS X Mavericks (10.9) 上引发 stdio.h not found 错误

c# - 谁能一眼就告诉我为什么这个 Action 这么慢?

python - django-taggit 公共(public)标签仅从基本 url 可见

python - 如何在django中生成临时文件然后销毁