I'm using Django Rest Framework's Generics (generics.ListCreateAPIView), when I make a POST request I get a response of Http code (200/400/..etc.) and a JSON showing the posted data, I need to know how can I override the response to get a custom response.
Note that I use
def perform_create(self,serializer):
return Response(<my response>)
to override the POST request handling but I still get the same response
The response from perform_create is ignored.
You'll likely want to override the create method using the mixins as example
Related
I am trying to implement sample spring boot project and to ensure my endpoints are working properly, i'm using POSTMAN. When using POSTMAN , I am not able to see the response(i.e in Pretty) for a POST request. But the Status is 200 OK and I am able to see the result using GET request.
No Pretty response for POST request
GET Response ensuring that the previous POST request works fine
And my controller code is the following
#PostMapping("/message")
public Message createMessage(#RequestBody Message message)
{
return service.createMessage(message);
}
Can anyone help me to find out why I am not able to see the result while using POST method please?
Like Rafael says it is good to return a Response with the object entity. I haven't been working with Spring myself but with JavaEE and in JavaEE it is perfectly possible to return the object directly without using a Response. I use Responses anyways though, because it is much nicer to work with, and you can create your own custom responses and status codes.
Maybe check if your createUser service actually returns a message.
I don't know much about Spring, but usually what works for me is using a ResponseEntity as the object returned by the function. Also, maybe you should use #RestController as the annotation to your class controller
#PostMapping("/message")
public ResponseEntity<Message> createMessage(#RequestBody Message message)
{
Message msg = service.createMessage(message);
return ResponseEntity.ok(msg);
}
I have started using WebClient in my Spring boot project recently.
Can somebody throw some light on the differences/usages between exchange and retrieve method in WebClient.
I undertand that exchange returns Mono<ClientResponse> and retrieve returns ResponseSpec, I just want to know when/why I should use each one of them.
Much Thanks.
Adding to #JArgente's answer.
According to the official documentation of the retrieve() method:
Perform the HTTP request and retrieve the response body.
...
This method is a shortcut to using exchange() and decoding the response body through
ClientResponse.
and the exchange() method
Perform the HTTP request and return a ClientResponse with the response status and headers. You can then use methods of the response to consume the body:
The retrieve() method decodes the ClientResponse object and hands you the ready-made object for your use. It doesn't have a very nice api for handling exceptions.
However on the other hand the exchange() method hands you the ClientResponse object itself along with the response status and headers. With exchange method you get fine grained control over your response objects and a better way to handle the response object and the exceptions.
If you just want to consume some api go with retrieve().
If you want a better control over your response objects, headers and exceptions, go with exchange().
Update 1
Starting from Spring 5.3, the exchange() method is deprecated due to possible memory/connection leaks. exchangeToMono() or exchangeToFlux() can be used instead.
Thanks #rhubarb for the update.
According to spring Webclient api documentation the difference between the two is that exchange retrieve in addition to the body other http response information like headers and status, while retrieve only returns body information.
So If you only need the body information you should use retrieve, because it is a shortcut for exchange and then get the body, but if you need other information like http status you must use exchange.
I made API server with Django rest framework.
It is quite simple board, But I have some question about delete article.
When delete article, METHOD will be DELETE.
In previous code, I receive parameter(ex. password) to HTTP Body, and it works perfectly.
But after I inspect about this, DELETE Method with HTTP body is not a standard.
So, delete method with http body is bad havior?
Is there any solution about this?
I am unable to add a custom header to a response that is returned from render():
response = render(request, 'my_template.html', {'ctx1': 1, 'ctx2': 2})
response['My-Custom-Header'] = 'abc12345'
return response
This is a response to an Ajax request initiated using jQuery's $.get(). On Chrome, the response has the template rendered properly, but it does not have the custom header. If I print the response object before returning, I see that it has my custom header.
I thought the issue was because of this answer, so I added Django middleware to add a header to all responses:
class CustomHeaderMiddleware():
def process_response(self, request, response):
response['Access-Control-Expose-Headers'] = 'My-Custom-Header'
return response
All my responses now have header Access-Control-Expose-Headers: My-Custom-Header, but I still see this issue.
My request is local; I'm using the Django development web server.
If I send a non-Ajax GET request, whatever custom header I add in Django is visible in the response on Chrome. So this issue seems to be limited to Ajax requests.
I am using Django 1.11.4 and Python2.
[edit]
If I capture the response in WireShark, I see that it does not have My-Custom-Header. Furthermore, if I add a custom header to the response dictionary in the above middleware, the header shows up in Wireshark and is visible in Chrome. So this seems to be a Django issue with responses to Ajax requests.
This issue is unrelated to Ajax. I was calling my view using this template tag. The custom response headers are lost because of line 32 in that code.
In my application there is a client and a WCf REST service. For invoking some wcf service the client is doing an http POST even though the service is a GET.
i do not want to do any changes in the client or the service.
So is there a way where i can convert this POST request to GET and add the data coming in as the POST to the URL and invoke the REST service.
Thanks in advance.
You can use URL Rewrite to issue 3xx Redirect which will use GET method, but you will loose all POST data.
The only safe way known to me is to rewrite POST request to some another custom page, where you:
collect all POST data/variables;
convert them into GET variables (assemble proper GET request);
issue 301 (or 302) Redirect to the proper URL (it will have all POST data sent as GET variables).
Such rewrite to custom page should be easy -- you need to check what method is used (POST or GET) and only invoke it on POST. The rest will be handled in that post-to-get script.
The reason for all of this complexity is the difference in how POST and GET requests work: with GET all data is sent as part of URL while POST uses request body to transfer variable's data.