nestjs swagger with formdata and request body Dto giving error - nestjs-swagger

https://github.com/nestjs/swagger/issues/2005
nestjs swagger with formdata and request body Dto

Related

Spring Framework WebFlux Reactive Programming

I am trying to send an object to the endpoint but I do not understand why I can't do it with .get(), why .post() has to be used? What if the endpoint method takes an object and does something with it and returns an object? I may want to send an object to the endpoint which takes the object as an argument. Is there a way to do it? How to pass a customer object to getCustomer() endpoint.
WebClient.create("http://localhost:8080")
.get()//why this can not be used? why post has to be used?
.uri("client/getCustomer")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(customer)//with .get() body cannot be passed.
.retrieve()
.bodyToMono(Customer.class);
#GET
#Path("/getCustomer")
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public Customer getCustomer(Customer customer) {
//do something
return customer;
}
Edited
In GET methods, the data is sent in the URL. just like:
http://www.test.com/users/1
In POST methods, The data is stored in the request body of the
HTTP request.
Therefore we should not expect .get() method to have .bodyValue().
Now if you wanna send data using GET method, you should send them in the URL, like below snippet
WebClient.create("http://localhost:8080")
.get()
.uri("client/getCustomer/{customerName}" , "testName")
.retrieve()
.bodyToMono(Customer.class);
Useful Spring webClient sample:
spring 5 WebClient and WebTestClient Tutorial with Examples
Further information about POST and GET
HTTP Request Methods

Spring WebClient returns Lambda instead of Data from Rest Service

I am calling a Rest Service with a WebClient but I only get a response back which is the following:
Object is reactor.core.publisher.LambdaSubscriber#4681c175
I was hoping to get a JSON string back. When I call the service with a RestTemplate I get the expected JSON string back. Here is the code I use to make the call that returns the Lambda object back:
Object objj= WebClient.create("http://localhost/printer-service/jobs")
.get()
.accept(MediaType.APPLICATION_STREAM_JSON)
.exchange()
.flatMapMany(cr -> cr.bodyToFlux(String.class))
.subscribe(System.out::println);
System.out.println("Object is "+objj);
Why is this call not returning the JSON string?

Asp.net web api IHttpActionResult springboot Java Equivalent

hi I am new in springboot and I want to develop rest api in springboot.
In .net web api IHttpActionresult type used to return entity and httpstatuscode in same time,is there any equivalent in spring boot
Use Spring's Class ResponseEntity<T>. It allows you to add an Object and a Response Status and send to the user.
Example from ResponseEntity docs:
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setLocation(location);
responseHeaders.set("MyResponseHeader", "MyValue");
return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
You see how ResponseEntity accepts the body of the response as its first argument, the headers of the response as the second and the HTTP status as the third.

401 Error accessing Rest API passing get_cookie

I am accessing an API which expects me to make a POST call to their Login endpoint and then make subsequent calls to get data. The call I make to their login endpoint works. But the subsequent call to get data throws a 401 error. This is the error I get
org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:615)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:573)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:544)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:465)
at com.wsc.eventconsumer.phonex.api.InventoryServiceImpl.getProducts(InventoryServiceImpl.java:70)
Now this works fine when I use Postman and the Restlet client. I noticed that in Restlet client the GET call to the API passes a cookie that was set by the API in the login call. I tried passing the response headers from the login call in the GET request. But I still get a 401. I am using the standard resttemplate configuration.
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
HttpEntity<Credentials> entity = new HttpEntity<Credentials>(credentials, headers);
ResponseEntity<String> response = restTemplate.exchange(loginEndpoint, HttpMethod.POST, entity, String.class);
HttpHeaders rHeaders = response.getHeaders();
HttpEntity<String> request = new HttpEntity<String>(rHeaders);
ResponseEntity<Product[]> response2 = restTemplate.exchange(productEndpoint,
HttpMethod.GET, request, Product[].class);

How to send MediaType.APPLICATION_FORM_URLENCODED and MediaType.APPLICATION_JSON in same request body using RestTemplate

I need to send "client_id" and "client_secret" parameters in request body as MediaType.APPLICATION_FORM_URLENCODED and another object as MediaType.APPLICATION_JSON in same RestTemplate request.
Is it possible?

Resources