A little complex path matching in Akka HTTP
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
add a comment |
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
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
add a comment |
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
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
scala akka-http
asked Nov 16 '18 at 12:19
Fede E.Fede E.
4421614
4421614
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
...
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
...
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
add a comment |
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
...
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
add a comment |
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
...
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
...
answered Nov 16 '18 at 14:38
C4storC4stor
6,11532138
6,11532138
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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