Reputation: 6275
There is an endpoint written in a Micronaut application like so:
@Post("/{someId}:verb")
I want to make a POST request to it from POSTMAN but not sure how I hit that? So far I have tried:
POST http://localhost:8080/1234/verb // 404 (Not Found)
POST http://localhost:8080/1234:verb // this combines ':' with 1234 and that in turn fails that @Pattern validation that I have placed.
POST http://localhost:8080/1234 // 405 (Method Not Allowed)
Upvotes: 0
Views: 1249
Reputation: 1439
I hope I didn't misunderstand your question (--> You aren't looking for a default value for someId?), but the following code works for me with cURL (someId
can be a String or Integer - what you prefer more):
package com.example;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.PathVariable;
import io.micronaut.http.annotation.Post;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Controller("/")
public class DemoController {
private static final Logger logger = LoggerFactory.getLogger(DemoController.class);
@Post(value = "/{someId}:verb", produces = MediaType.TEXT_PLAIN)
public String postMethod(@PathVariable String someId) {
String message = "You called me with the value: " + someId;
logger.info(message);
return message;
}
}
cURL:
.\curl.exe -X POST http://localhost:8080/WeLoveCake42:verb
.\curl.exe -X POST http://localhost:8080/12345678:verb
Output:
20:14:42.027 [default-nioEventLoopGroup-1-5] INFO com.example.DemoController - You called me with the value: WeLoveCake42
20:15:37.989 [default-nioEventLoopGroup-1-7] INFO com.example.DemoController - You called me with the value: 12345678
In case I misunderstood your question:
defaultValue
on @PathVariable
someId
@Nullable
I guess the routing/dispatcher is not flexible enough to detect this pattern (Maybe it might even lead to a non-determinstic behaviour, so it's quite understandable).
Upvotes: 1
Reputation: 6069
I'm quoting from the Micronaut documentation (highlight added by me):
Remember that to specify a default value in a placeholder expression, you should use the colon
:
character, however if the default you are trying to specify has a colon then you should escape the value with back ticks.
So probably you need to replace the verb
by a default value, if someId
is not set.
When someId
is provided in your POST request, the method will use someId
. Otherwise it will use the default value, which is verb
as per your example. So if you are testing with POSTMAN, either set the id or leave it empty, to test the default value.
Upvotes: 2