Reputation: 233
I have a model
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)
CreateAPIView
class Item_Create_APIView(CreateAPIView):
authentication_classes = [SessionAuthentication, BasicAuthentication, JSONWebTokenAuthentication]
permission_classes = [IsAuthenticated]
serializer_class = ItemCreateSerializer
queryset = Item.objects.all()
And Serializer
I am trying to test the createView post via curl or http in the terminal but no luck so far. I am using python3.5 and Django==1.8.10 and Django Rest Framework with JWT token authentication. I can login and get a token but I don't know how to format the json data to the baseusrl/create_item endpoint to test the Create Post call via curl to see if the API works.
Anyone with guidance on this? Thanks
I tried this and it didn't work:
curl —X POST H ”Authorization: JWT token” http://localhost:8000/create_item/ ‘{“key”:”val”}’
I have tried this format and got "no url specified" while the url is in the call
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/
any ideas?
Upvotes: 3
Views: 12621
Reputation: 233
After much deliberation, I resulted to a double-dose of coffee and got the proper way to implement the POST calls with JWT authentication. There is one caveat in that I had to put single quotes around the protected url to make a successful POST with the record persisting to the database. I also opted for not passing the data in JSON format as Key Value pairs since the JSON renderer will handle it.
For anyone else who encounters the same problem the syntax that worked is:
curl -H "Authorization: JWT YourToken" -d "field=value&field=value" 'http://127.0.0.1:8000/protected_url'
Upvotes: 4
Reputation: 7734
Did you try the format specified i the official documentation:
curl -X GET http://127.0.0.1:8000/api/example/ -H 'Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b'
Upvotes: 2