【发布时间】:2016-12-18 23:27:13
【问题描述】:
我有模特
class Item(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
otheritemmodel = models.ForeignKey(Model, on_delete=models.CASCADE)
uuid = models.CharField(max_length=256, unique=True)
floatitem = models.FloatField()
relateditem = models.ForeignKey(RelatedItem, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
subcategory = models.ForeignKey(SubCategory, on_delete=models.CASCADE)
character = models.CharField(max_length=500, null=True, blank=True)
zipcode = models.IntegerField()
city = models.CharField(max_length=100)
state = models.CharField(max_length=100)
active = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
创建API视图
class Item_Create_APIView(CreateAPIView):
authentication_classes = [SessionAuthentication, BasicAuthentication, JSONWebTokenAuthentication]
permission_classes = [IsAuthenticated]
serializer_class = ItemCreateSerializer
queryset = Item.objects.all()
和序列化器
我正在尝试在终端中通过 curl 或 http 测试 createView 帖子,但到目前为止还没有运气。我正在使用 python3.5 和 Django==1.8.10 以及带有 JWT 令牌身份验证的 Django Rest Framework。我可以登录并获取令牌,但我不知道如何将 json 数据格式化为 baseusrl/create_item 端点以通过 curl 测试 Create Post 调用以查看 API 是否有效。
有人对此有指导吗?谢谢
我试过了,还是不行:
curl —X POST H ”Authorization: JWT token” http://localhost:8000/create_item/ ‘{“key”:”val”}’
我已经尝试过这种格式,并且在调用时得到“未指定 url”
curl -X POST -H "Authorization: JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJleHAiOjE0NzEwMjc4MjIsImVtYWlsIjoicGF0b3JlQHByb3Rvbm1haWwuY2giLCJ1c2VybmFtZSI6InBhdG9yZUBwcm90b25tYWlsLmNoIn0.seO6ayMayJ8hwGf3Tlqwu95cZOw5VyFGVqsT5LymdJw" -H "Content-Type: application/json" -d “user=37&otheritmemodel=3&uuid=4454353454983543434&floatitem=40.0&relateditem=28&category=1&subcategory=1&character=jim&zipcode=11111&city=City&state=State” http://localhost:8000/user-api/create_item/
有什么想法吗?
【问题讨论】:
-
你得到什么回应?
-
'curl: (6) 无法解析主机:-X curl: (6) 无法解析主机:POST
301 Moved Permanently head>301 永久移动
nginx curl: (6 ) 无法解析主机:“授权
标签: python django curl terminal django-rest-framework