0

I think that I might have misunderstood something. I created Django Rest Framework app. I have working api with tokens, query_params etc.

Everything is working if I make a curl like that:

curl -X "GET" http://localhost:8000/api/results 
-H "Content-Type: application/json" 
-H "Authorization: Token eeea084811d9213aa3803946e4464758d66966bc" 
--data 'make=test'

It is actually returning proper records. But often, on other websites I see that they are implementing their api for users in this format:

http://www.example.com/?apikey=[yourkey]&

I tried to look up converting curl to http but I didn't find anything useful for me and now I am confused. Maybe my api works only with curl currently? Maybe this "http call" needs to be somehow implemented/coded additionally?

I suspect that this question is stupid but I don't know yet proper keywords to google it unfortunately.

2 Answers 2

0

@Peksio- Actually, it is not that hard. You have two options if you are using Django

  1. The typical Django way
  2. The DRF way

You can create a Django view (GET/POST), wire up a models.py, and then add in urls.py. If you want it to be an API then you can simply call the endpoint depending on the HTTP method and Voila. You are done

The DRF way is even simpler.

  1. Install Django rest framework
  2. Create a file called serializers.py (to serialize your model)
  3. Create a viewset which is kind of like the django view.Only that you have to add in a queryset, serializer
  4. Add in a router or just a URL in urls.py and you are all done.

If you are using Token Based Authentication then DRF supports that too https://www.django-rest-framework.org/api-guide/authentication/

For JWT based authentication https://simpleisbetterthancomplex.com/tutorial/2018/12/19/how-to-use-jwt-authentication-with-django-rest-framework.html

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, that was informative. I also managed to find a fix for it. It seems I had troubles to find it because DRF by default not do not support auth through query string parameters, but this post explains how to get it to work - stackoverflow.com/questions/29433416/… Still, thank you for your explanation :)
0

I think you have a bit of a misunderstanding of what curl and HTTP requests actually are.

Both curl and web browsers make HTTP request calls the same way. For example when you type http://www.example.com/?apikey=123 into your browser then the browser actually makes a HTTP GET request to http://www.example.com/?apikey=123. The curl equivalent for this would be: curl -X "GET" http://www.example.com/?apikey=123 which sends the same HTTP GET request.

Now that that is clear lets see the difference between the two formats you provided.

For starters you need to have a basic understanding of what HTTP request actually is. Your typical HTTP request which would go to an API endpoint consists of the following:

  • method - GET, POST, PATCH, ...
  • URL - http://www.example.com/
  • query params (which are actually part of the URL) - apikey=123 or ordering=created_at for example
  • headers - Content-Type: application/json or Authorization: apikey 123 for example
  • body - any kind of data (can be form like, json, raw text, binary, just about anything)

Now the only difference in the two formats you provided is placement of authorization value (Token, apikey). In your first format it is place in the headers part of the HTTP request as a value of Authorization key. In your second format it is placed in the query params part of the HTTP request as a value of apikey.

Hope that clears it up a bit.

1 Comment

Thank you for your answer, that was informative. I also managed to find a fix for it. It seems I had troubles to find it because DRF by default not do not support auth through query string parameters, but this post explains how to get it to work - stackoverflow.com/questions/29433416/… Still, thank you for your explanation :)

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.