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

Closed
opened 2025-12-29 15:25:52 +01:00 by adam · 4 comments
Owner

Originally created by @xayaraj on GitHub (Jun 22, 2023).

Originally assigned to: @StefH on GitHub.

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.

Originally created by @xayaraj on GitHub (Jun 22, 2023). Originally assigned to: @StefH on GitHub. ### 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.
adam added the question label 2025-12-29 15:25:52 +01:00
adam closed this issue 2025-12-29 15:25:52 +01:00
Author
Owner

@StefH commented on GitHub (Jun 22, 2023):

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

@StefH commented on GitHub (Jun 22, 2023): The `{id}` does not work, you can just use a wildcard string: `/api/users/*`. Can you try that ?
Author
Owner

@xayaraj commented on GitHub (Jun 23, 2023):

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?

@xayaraj commented on GitHub (Jun 23, 2023): 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?
Author
Owner

@StefH commented on GitHub (Jun 23, 2023):

That would be possible using:

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

@StefH commented on GitHub (Jun 23, 2023): That would be possible using: `request.PathSegments.[<n>]` --> URL path segment (zero indexed) e.g. request.PathSegments.[2] - https://github.com/WireMock-Net/WireMock.Net/wiki/Response-Templating#the-request-model
Author
Owner

@xayaraj commented on GitHub (Jun 23, 2023):

Awesome. Thank you.

@xayaraj commented on GitHub (Jun 23, 2023): Awesome. Thank you.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#522