HTTP methods for the Django Rest Framework Viewset - django

I'm working on django rest framework viewset and want to know which HTTP methods map to the functions:
1.list()
2.create()
3.retrieve()
4.update()
5.partial_update()
6.destroy()
I have searched a lot but I didn't got the specific answer to my question.
So I just want to know which http method maps all the above listed functions
thanks in advance!!

You can see Django Rest Framework code and see in routers.py file methods mapping, e.g. in SimpleRouter:
GET: list() and retrieve()
POST: create()
PUT: update()
PATCH: partial_update()
DELETE: destroy()

In simple way you can say:
1.list(): HTTP Get
2.create(): HTTP Post
3.retrieve(): HTTP Get
4.update(): HTTP Put
5.partial_update(): HTTP Patch
6.destroy(): HTTP Delete

Related

In Postman how to mock a post API call to return reponses based on the request body?

I'm trying to leverage Postman's mock server feature to mock an API that my application calls.
This is a Post request. I have gone through the documentation and as advised I have saved the responses as examples.
When I try hit the mock URL I get the postman error response
Here is my setup -
My Collection with saved examples
MY mock server
After going through your query, I can see that you're trying to match an example based on the body passed with the request.
To match an example based on the request body, you can leverage the body matching feature of mock servers by:
Enabling the body matching feature from the mock edit page (Reference: https://learning.postman.com/docs/designing-and-developing-your-api/mocking-data/setting-up-mock/#matching-request-body-and-headers).
OR
Passing an additional x-mock-match-request-body header with value as true along with your mock request to get the desired results.
You can find more information on how to use body matching feature with mock servers here: https://learning.postman.com/docs/designing-and-developing-your-api/mocking-data/matching-algorithm/#6-check-for-header-and-body-matching.
Do let me know if this doesn't solve your issue. In that case, it would be helpful if you can share the mock request that you're sending to get the response.

Postman Mock Server matching algorithm logic for request body param

I have two scenarios for the following API URL.
POST http://{{ip_port}}/oauth/token
When I put the user name and password correctly, it should return
200 and mock json response.
When I put user name and password incorrectly, it should return 401 and mocked json(error).
In Postman Mock server, I noticed that there is no matching algorithm logic for request param.
I want to filter by request param and return related mock responses. I don't want to add two URLs(/token and /failedtoken) for above scenarios.
Currently Postman only support three logic for matching algorithm logic.
Properly formatted responses
HTTP method
Filter by URL
Is there any way to add only one URL for many scenarios in Postman Mock Server?
Postman Mock Server now supports matching by request body. You can use it by specifying a custom header (x-mock-match-request-body to true).
You can also check out an example that demonstrates how this feature works by going to New->Templates and searching for Request Body Matching.

PUT, GET ,POST ,DELETE methods using djangorest framework

I'm using django rest framework , I use the post and get methods and it works, but I didn't understand how to use PUT and DELETE, do I use it in the html forms like : method='PUT' ? but i read that the browsers assimilated it to a GET method , do I write fonctions in my code for PUT and DELETE??
-I read many articles about rest and restful and I didn't understand the difference between it some people says that its the same , and others no but dont clarify,when I use POST and GET can I say that it's RESTFUL
thank you
Unless there's been a recent development, HTML forms do not support PUT nor DELETE methods. (GET, POST, PUT, and DELETE methods are part of HTTP, not HTML, more on this topic in this question)
However you can send PUT and DELETE requests using an HTTP client, e.g. Python has a library called requests you can use to send requests. Or if you want to do so from the front-end, e.g. from a browser, you can use JavaScript lib capable of sending out HTTP requests (or the more recent fetch() that comes with modern browsers, or its polyfill for older browsers)
e.g.
>>> import requests
>>> req = requests.request('PUT', 'http://yourapi/resource')
<Response [200]>

using httplib2 Python to create REST POST listener

I found woefully few samples of httplib2 using the following template defined in the httplib2 documents.
The following (GETs) work (and I now need to implement POST methods using it)
URLS = (
'/', 'PingLocal',
'/ping', 'PingLocal',
'/ping/silo', 'PingSilo'
)
class PingLocal(object):
def GET(self):
return json.dumps({'time': time.strftime('%m/%d/%Y %I:%M:%S'), 'message': 'XYZ Server Responding to Ping'})
How to implement POST methods and pass data (body) to the post request?
A few examples I did come across do not use this suggested
"URLS-list / Classes" model.
Any pointers appreciated. Thank you.
I made a mistake.
In the code I am implementing the REST Server AND in the implemented methods - I am in turn making REST client calls to remote URIs.
I am using httplib2 to make those REST client calls (as the library documentation clearly mentions - its the client library).
I am in turn using web.py module to implement the REST server - and found the relevant documentation there.
Thank you

Query string http param in play framework 1.2.5

i need to add a route for following syntax
/bidding/12345?access_token=ACCESS_TOKEN
in my routes file i add like this
GET /bidding/{id} Application.bidding
i try to send request with query param /bidding/12345?access_token=ACCESS_TOKEN like above but play response is 404 (not found), i was using play framework 1.2.5
thanks for any response to my question, and sorry for my bad English
The URL you are trying to access is not represented correctly in the routes file.
If you would like to access the URL:
/bidding/12345?access_token=ACCESS_TOKEN
The routes file entry will need to be changed to the following:
GET /bidding Application.bidding
Play will use its magic to route that GET request to the following method in the Application controller:
public static void bidding(String access_token)
Your route configuration seems ok : are you sure you have a valid public static method bidding in your Application class ? Could you post the source of this method ?