3

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

Create 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?

2
  • what response are u getting ? Commented Aug 12, 2016 at 4:12
  • 'curl: (6) Could not resolve host: —X curl: (6) Could not resolve host: POST <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx</center> </body> </html> curl: (6) Could not resolve host: ”Authorization Commented Aug 12, 2016 at 4:23

2 Answers 2

4

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'
Sign up to request clarification or add additional context in comments.

1 Comment

But how do you get first JWT Token, meaning how do you get token by passing username and password. I was looking for this and here is it curl -d "username=admin&password=secret" -X POST 'http://localhost:8000/api/token/'
2

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'

1 Comment

I logged in and got a new token then tried that format and got 'credentials not provided' curl -X GET 127.0.0.1:8000/user-api/create_item -H 'Authorization: Token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InBhdG9yZUBwcm90b25tYWlsLmNoIiwidXNlcl9pZCI6MSwiZXhwIjoxNDcwOTc2MTMyLCJlbWFpbCI6InBhdG9yZUBwcm90b25tYWlsLmNoIn0.rIbezfMm_PGOAxk-lD6o_XvISe_uKr7VWGMiqjRqvms' {"detail":"Authentication credentials were not provided."}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.