A little complex path matching in Akka HTTP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm new to Akka HTTP, and trying to write my first API. The routing DSL seems a little confusing.



I managed to match the following:



/channel
/channel/channelName



But now I need to match the following:



/channel/channelName/channelAction



And I can't get it to work.



I currently have:



private val routes: Route =
path("channel") {
get {
reportAllChannelsStatus()
}
} ~
pathPrefix("channel" / Remaining) { channelName =>
get {
singleChannelRequest(channelName, status)
} ~
post {
entity(as[ChannelRequest]) { request =>
singleChannelRequest(channelName, request.channelAction)
}
}
} ~
completeWith404()


I want to add get and post for /channel/channelName/channelAction



Any idea how is this done? (extract both channelName and channelAction)










share|improve this question























  • FWIW, if you have the choice, you might want to consider Play for your web apps. I know it doesn't answer your question, but it's much better suited.

    – erip
    Nov 16 '18 at 12:39













  • @erip I am implementing an HTTP API on an already huge system that uses Akka Actors, and Akka HTTP Client to make external requests, so I decided to go with Akka HTTP, which everything needed in already included in the project.

    – Fede E.
    Nov 16 '18 at 12:53











  • Every other thing in the Lightbend stack ues Akka actors extensively, Play included.

    – erip
    Nov 16 '18 at 12:59











  • Akka http is totally fine for API writing, and I believe you'll find no more value for writing API in Play. For full web backend (like e.g if you need to render web pages), maybe you'll want to go Play.

    – C4stor
    Nov 16 '18 at 14:40




















1















I'm new to Akka HTTP, and trying to write my first API. The routing DSL seems a little confusing.



I managed to match the following:



/channel
/channel/channelName



But now I need to match the following:



/channel/channelName/channelAction



And I can't get it to work.



I currently have:



private val routes: Route =
path("channel") {
get {
reportAllChannelsStatus()
}
} ~
pathPrefix("channel" / Remaining) { channelName =>
get {
singleChannelRequest(channelName, status)
} ~
post {
entity(as[ChannelRequest]) { request =>
singleChannelRequest(channelName, request.channelAction)
}
}
} ~
completeWith404()


I want to add get and post for /channel/channelName/channelAction



Any idea how is this done? (extract both channelName and channelAction)










share|improve this question























  • FWIW, if you have the choice, you might want to consider Play for your web apps. I know it doesn't answer your question, but it's much better suited.

    – erip
    Nov 16 '18 at 12:39













  • @erip I am implementing an HTTP API on an already huge system that uses Akka Actors, and Akka HTTP Client to make external requests, so I decided to go with Akka HTTP, which everything needed in already included in the project.

    – Fede E.
    Nov 16 '18 at 12:53











  • Every other thing in the Lightbend stack ues Akka actors extensively, Play included.

    – erip
    Nov 16 '18 at 12:59











  • Akka http is totally fine for API writing, and I believe you'll find no more value for writing API in Play. For full web backend (like e.g if you need to render web pages), maybe you'll want to go Play.

    – C4stor
    Nov 16 '18 at 14:40
















1












1








1


1






I'm new to Akka HTTP, and trying to write my first API. The routing DSL seems a little confusing.



I managed to match the following:



/channel
/channel/channelName



But now I need to match the following:



/channel/channelName/channelAction



And I can't get it to work.



I currently have:



private val routes: Route =
path("channel") {
get {
reportAllChannelsStatus()
}
} ~
pathPrefix("channel" / Remaining) { channelName =>
get {
singleChannelRequest(channelName, status)
} ~
post {
entity(as[ChannelRequest]) { request =>
singleChannelRequest(channelName, request.channelAction)
}
}
} ~
completeWith404()


I want to add get and post for /channel/channelName/channelAction



Any idea how is this done? (extract both channelName and channelAction)










share|improve this question














I'm new to Akka HTTP, and trying to write my first API. The routing DSL seems a little confusing.



I managed to match the following:



/channel
/channel/channelName



But now I need to match the following:



/channel/channelName/channelAction



And I can't get it to work.



I currently have:



private val routes: Route =
path("channel") {
get {
reportAllChannelsStatus()
}
} ~
pathPrefix("channel" / Remaining) { channelName =>
get {
singleChannelRequest(channelName, status)
} ~
post {
entity(as[ChannelRequest]) { request =>
singleChannelRequest(channelName, request.channelAction)
}
}
} ~
completeWith404()


I want to add get and post for /channel/channelName/channelAction



Any idea how is this done? (extract both channelName and channelAction)







scala akka-http






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 12:19









Fede E.Fede E.

4721615




4721615













  • FWIW, if you have the choice, you might want to consider Play for your web apps. I know it doesn't answer your question, but it's much better suited.

    – erip
    Nov 16 '18 at 12:39













  • @erip I am implementing an HTTP API on an already huge system that uses Akka Actors, and Akka HTTP Client to make external requests, so I decided to go with Akka HTTP, which everything needed in already included in the project.

    – Fede E.
    Nov 16 '18 at 12:53











  • Every other thing in the Lightbend stack ues Akka actors extensively, Play included.

    – erip
    Nov 16 '18 at 12:59











  • Akka http is totally fine for API writing, and I believe you'll find no more value for writing API in Play. For full web backend (like e.g if you need to render web pages), maybe you'll want to go Play.

    – C4stor
    Nov 16 '18 at 14:40





















  • FWIW, if you have the choice, you might want to consider Play for your web apps. I know it doesn't answer your question, but it's much better suited.

    – erip
    Nov 16 '18 at 12:39













  • @erip I am implementing an HTTP API on an already huge system that uses Akka Actors, and Akka HTTP Client to make external requests, so I decided to go with Akka HTTP, which everything needed in already included in the project.

    – Fede E.
    Nov 16 '18 at 12:53











  • Every other thing in the Lightbend stack ues Akka actors extensively, Play included.

    – erip
    Nov 16 '18 at 12:59











  • Akka http is totally fine for API writing, and I believe you'll find no more value for writing API in Play. For full web backend (like e.g if you need to render web pages), maybe you'll want to go Play.

    – C4stor
    Nov 16 '18 at 14:40



















FWIW, if you have the choice, you might want to consider Play for your web apps. I know it doesn't answer your question, but it's much better suited.

– erip
Nov 16 '18 at 12:39







FWIW, if you have the choice, you might want to consider Play for your web apps. I know it doesn't answer your question, but it's much better suited.

– erip
Nov 16 '18 at 12:39















@erip I am implementing an HTTP API on an already huge system that uses Akka Actors, and Akka HTTP Client to make external requests, so I decided to go with Akka HTTP, which everything needed in already included in the project.

– Fede E.
Nov 16 '18 at 12:53





@erip I am implementing an HTTP API on an already huge system that uses Akka Actors, and Akka HTTP Client to make external requests, so I decided to go with Akka HTTP, which everything needed in already included in the project.

– Fede E.
Nov 16 '18 at 12:53













Every other thing in the Lightbend stack ues Akka actors extensively, Play included.

– erip
Nov 16 '18 at 12:59





Every other thing in the Lightbend stack ues Akka actors extensively, Play included.

– erip
Nov 16 '18 at 12:59













Akka http is totally fine for API writing, and I believe you'll find no more value for writing API in Play. For full web backend (like e.g if you need to render web pages), maybe you'll want to go Play.

– C4stor
Nov 16 '18 at 14:40







Akka http is totally fine for API writing, and I believe you'll find no more value for writing API in Play. For full web backend (like e.g if you need to render web pages), maybe you'll want to go Play.

– C4stor
Nov 16 '18 at 14:40














1 Answer
1






active

oldest

votes


















2














You can match



path("channel" / Segment / Segment){
(channelName, channelAction) => ...
}


Be aware that for different types of arguments, you'll have to match different things. Segment is for String, IntNumber would be for Int ...






share|improve this answer
























  • This worked! Thanks!

    – Fede E.
    Nov 16 '18 at 15:09











  • If I may suggest it, I'd replace in your codebase Remaining with Segment for cases where you know you just want to match a String. Remaining will happily give you slashes and stuff, and make it difficult to control sub levels matching. In any case, you're welcome :-)

    – C4stor
    Nov 16 '18 at 15:12











  • I did! Thanks. This is what I ended with: pastebin.com/e3T9pSKk

    – Fede E.
    Nov 16 '18 at 15:34












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337798%2fa-little-complex-path-matching-in-akka-http%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can match



path("channel" / Segment / Segment){
(channelName, channelAction) => ...
}


Be aware that for different types of arguments, you'll have to match different things. Segment is for String, IntNumber would be for Int ...






share|improve this answer
























  • This worked! Thanks!

    – Fede E.
    Nov 16 '18 at 15:09











  • If I may suggest it, I'd replace in your codebase Remaining with Segment for cases where you know you just want to match a String. Remaining will happily give you slashes and stuff, and make it difficult to control sub levels matching. In any case, you're welcome :-)

    – C4stor
    Nov 16 '18 at 15:12











  • I did! Thanks. This is what I ended with: pastebin.com/e3T9pSKk

    – Fede E.
    Nov 16 '18 at 15:34
















2














You can match



path("channel" / Segment / Segment){
(channelName, channelAction) => ...
}


Be aware that for different types of arguments, you'll have to match different things. Segment is for String, IntNumber would be for Int ...






share|improve this answer
























  • This worked! Thanks!

    – Fede E.
    Nov 16 '18 at 15:09











  • If I may suggest it, I'd replace in your codebase Remaining with Segment for cases where you know you just want to match a String. Remaining will happily give you slashes and stuff, and make it difficult to control sub levels matching. In any case, you're welcome :-)

    – C4stor
    Nov 16 '18 at 15:12











  • I did! Thanks. This is what I ended with: pastebin.com/e3T9pSKk

    – Fede E.
    Nov 16 '18 at 15:34














2












2








2







You can match



path("channel" / Segment / Segment){
(channelName, channelAction) => ...
}


Be aware that for different types of arguments, you'll have to match different things. Segment is for String, IntNumber would be for Int ...






share|improve this answer













You can match



path("channel" / Segment / Segment){
(channelName, channelAction) => ...
}


Be aware that for different types of arguments, you'll have to match different things. Segment is for String, IntNumber would be for Int ...







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 14:38









C4storC4stor

6,21032138




6,21032138













  • This worked! Thanks!

    – Fede E.
    Nov 16 '18 at 15:09











  • If I may suggest it, I'd replace in your codebase Remaining with Segment for cases where you know you just want to match a String. Remaining will happily give you slashes and stuff, and make it difficult to control sub levels matching. In any case, you're welcome :-)

    – C4stor
    Nov 16 '18 at 15:12











  • I did! Thanks. This is what I ended with: pastebin.com/e3T9pSKk

    – Fede E.
    Nov 16 '18 at 15:34



















  • This worked! Thanks!

    – Fede E.
    Nov 16 '18 at 15:09











  • If I may suggest it, I'd replace in your codebase Remaining with Segment for cases where you know you just want to match a String. Remaining will happily give you slashes and stuff, and make it difficult to control sub levels matching. In any case, you're welcome :-)

    – C4stor
    Nov 16 '18 at 15:12











  • I did! Thanks. This is what I ended with: pastebin.com/e3T9pSKk

    – Fede E.
    Nov 16 '18 at 15:34

















This worked! Thanks!

– Fede E.
Nov 16 '18 at 15:09





This worked! Thanks!

– Fede E.
Nov 16 '18 at 15:09













If I may suggest it, I'd replace in your codebase Remaining with Segment for cases where you know you just want to match a String. Remaining will happily give you slashes and stuff, and make it difficult to control sub levels matching. In any case, you're welcome :-)

– C4stor
Nov 16 '18 at 15:12





If I may suggest it, I'd replace in your codebase Remaining with Segment for cases where you know you just want to match a String. Remaining will happily give you slashes and stuff, and make it difficult to control sub levels matching. In any case, you're welcome :-)

– C4stor
Nov 16 '18 at 15:12













I did! Thanks. This is what I ended with: pastebin.com/e3T9pSKk

– Fede E.
Nov 16 '18 at 15:34





I did! Thanks. This is what I ended with: pastebin.com/e3T9pSKk

– Fede E.
Nov 16 '18 at 15:34




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337798%2fa-little-complex-path-matching-in-akka-http%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python