WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.36k stars 200 forks source link

Path Variable id Not Working (ie. "/api/users/{id}")? #957

Closed xayaraj closed 1 year ago

xayaraj commented 1 year ago

Describe the bug

With the url /api/user/{id} endpoint where id is the path variable. I have created the WireMock.Net server to listen for the given url path for Http incoming request.

Server:

_server .Given(Request.Create().WithPath("/api/users/{id}").UsingGet()) .RespondWith(Response.Create().WithStatusCode(200).WithBody("User {id}"));

Client: var client = new HttpClient(); var response = await client.GetAsync($"{_server.Urls[0]}/api/users/123"); var content = await response.Content.ReadAsStringAsync();

Response receive is 404 Not found

It might a simple stupid mistake or something that I have missed. Any help is appreciated. Thanks.

StefH commented 1 year ago

The {id} does not work, you can just use a wildcard string: /api/users/*. Can you try that ?

xayaraj commented 1 year ago

It works. Thank you.

Is there a way to retrieve the id which is 123 value and respond it back in the body as shown above?

StefH commented 1 year ago

That would be possible using:

request.PathSegments.[<n>] --> URL path segment (zero indexed) e.g. request.PathSegments.[2]

xayaraj commented 1 year ago

Awesome. Thank you.