{"id":1515,"date":"2025-11-20T09:16:19","date_gmt":"2025-11-20T14:16:19","guid":{"rendered":"https:\/\/artfulmonkeys.xyz\/wp\/?p=1515"},"modified":"2025-11-20T10:49:43","modified_gmt":"2025-11-20T15:49:43","slug":"19-accessing-path-parameters","status":"publish","type":"post","link":"https:\/\/artfulmonkeys.xyz\/wp\/server-side\/19-accessing-path-parameters\/","title":{"rendered":"19. Accessing Path Parameters"},"content":{"rendered":"<p>We can create endpoints that require the client to include search criteria within the <em>path<\/em> (as opposed to including query strings). \u00a0For example, suppose the user (via the client) has access to their friend&#8217;s User <em>_id<\/em>s. \u00a0Then we can create an endpoint that allows the client to get the profile information of a user by passing the User _id in the path. \u00a0 For example the client could send a request using the following method and path where 69153ce6801518f4e58d51ae is a User _id.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">GET \/user\/69153ce6801518f4e58d51ae<\/pre>\n<p>The endpoint definition will include a placeholder (e.g. <strong>:id<\/strong>) in the path (<strong>\/user\/:id<\/strong>) and the handler can retrieve the value of the placeholder from a request using <strong>req.params.id<\/strong>. \u00a0<\/p>\n<p>A paths can have any number of parameter placeholders so long as the names of the placeholders are distinct.<\/p>\n<p>Below is code for an endpoint that allows a user to retrieve profile information using a User _id in the path. \u00a0Copy the code into your user.js router file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">router.get('\/user\/:id', auth, async (req, res) =&gt; {\r\n    try {\r\n        const user = await User.findById(req.params.id).lean()\r\n\r\n        if (!user) {\r\n            res.status(400).send()\r\n        }\r\n\r\n        res.send(user)\r\n    }\r\n    catch(err) {\r\n        console.log(err)\r\n        res.status(500).send(err)\r\n    }\r\n})<\/pre>\n<h3>Test With Postman and Push to GitHub<\/h3>\n<p>Create a request in Postman and test the endpoint. \u00a0When you&#8217;re satisfied that the endpoint works, push your code to GitHub.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We can create endpoints that require the client to include search criteria within the path (as opposed to including query strings). \u00a0For example, suppose the user (via the client) has access to their friend&#8217;s User _ids. \u00a0Then we can create an endpoint that allows the client to get the profile information of a user by [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1515","post","type-post","status-publish","format-standard","hentry","category-server-side"],"_links":{"self":[{"href":"https:\/\/artfulmonkeys.xyz\/wp\/wp-json\/wp\/v2\/posts\/1515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/artfulmonkeys.xyz\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artfulmonkeys.xyz\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artfulmonkeys.xyz\/wp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/artfulmonkeys.xyz\/wp\/wp-json\/wp\/v2\/comments?post=1515"}],"version-history":[{"count":6,"href":"https:\/\/artfulmonkeys.xyz\/wp\/wp-json\/wp\/v2\/posts\/1515\/revisions"}],"predecessor-version":[{"id":1521,"href":"https:\/\/artfulmonkeys.xyz\/wp\/wp-json\/wp\/v2\/posts\/1515\/revisions\/1521"}],"wp:attachment":[{"href":"https:\/\/artfulmonkeys.xyz\/wp\/wp-json\/wp\/v2\/media?parent=1515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artfulmonkeys.xyz\/wp\/wp-json\/wp\/v2\/categories?post=1515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artfulmonkeys.xyz\/wp\/wp-json\/wp\/v2\/tags?post=1515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}