Akka http add header to POST request with body
I have an akka http route that accepts a json in the body of the request. I am trying to test that route by using akka http test kit.
val header = RawHeader("Content-Type", "application/json")
Post("/tasks", trigger.asJson.noSpaces) ~> addHeader(header) ~>
addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
This test is failing with message
415 Unsupported Media Type was not equal to 200 OK
How do i properly add the content type header to the request?
scala unit-testing akka akka-http akka-testkit
add a comment |
I have an akka http route that accepts a json in the body of the request. I am trying to test that route by using akka http test kit.
val header = RawHeader("Content-Type", "application/json")
Post("/tasks", trigger.asJson.noSpaces) ~> addHeader(header) ~>
addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
This test is failing with message
415 Unsupported Media Type was not equal to 200 OK
How do i properly add the content type header to the request?
scala unit-testing akka akka-http akka-testkit
add a comment |
I have an akka http route that accepts a json in the body of the request. I am trying to test that route by using akka http test kit.
val header = RawHeader("Content-Type", "application/json")
Post("/tasks", trigger.asJson.noSpaces) ~> addHeader(header) ~>
addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
This test is failing with message
415 Unsupported Media Type was not equal to 200 OK
How do i properly add the content type header to the request?
scala unit-testing akka akka-http akka-testkit
I have an akka http route that accepts a json in the body of the request. I am trying to test that route by using akka http test kit.
val header = RawHeader("Content-Type", "application/json")
Post("/tasks", trigger.asJson.noSpaces) ~> addHeader(header) ~>
addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
This test is failing with message
415 Unsupported Media Type was not equal to 200 OK
How do i properly add the content type header to the request?
scala unit-testing akka akka-http akka-testkit
scala unit-testing akka akka-http akka-testkit
asked Jan 3 at 1:19
NiroNiro
158113
158113
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Let akka-http create RequestEntity
on its own instead of passing json as String
yourself.
You just need to pass trigger
as the second parameter of Post.apply
as is.
Post("/tasks", trigger) ~> addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
This will require a ToEntityMarshaller[Trigger]
available in your implicit context.
And you can add it in the same way as you do in you route definition by importing/extending de.heikoseeberger.akkahttpargonaut.ArgonautSupport
and argonaut CodecJson[Trigger]
if you use argonaut for example.
In case you want to pass arbitrary string value, do
Post("/tasks").withEntity(ContentTypes.`application/json`, someJsonAsString) ~> addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
This works but it will always send the request body with a proper json. I want to test the case where the json is invalid as well.
– Niro
Jan 3 at 10:16
You need to clearly state this in your question
– Ivan Stanislavciuc
Jan 3 at 10:20
@Niro check the edit
– Ivan Stanislavciuc
Jan 3 at 10:24
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%2f54015198%2fakka-http-add-header-to-post-request-with-body%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
Let akka-http create RequestEntity
on its own instead of passing json as String
yourself.
You just need to pass trigger
as the second parameter of Post.apply
as is.
Post("/tasks", trigger) ~> addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
This will require a ToEntityMarshaller[Trigger]
available in your implicit context.
And you can add it in the same way as you do in you route definition by importing/extending de.heikoseeberger.akkahttpargonaut.ArgonautSupport
and argonaut CodecJson[Trigger]
if you use argonaut for example.
In case you want to pass arbitrary string value, do
Post("/tasks").withEntity(ContentTypes.`application/json`, someJsonAsString) ~> addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
This works but it will always send the request body with a proper json. I want to test the case where the json is invalid as well.
– Niro
Jan 3 at 10:16
You need to clearly state this in your question
– Ivan Stanislavciuc
Jan 3 at 10:20
@Niro check the edit
– Ivan Stanislavciuc
Jan 3 at 10:24
add a comment |
Let akka-http create RequestEntity
on its own instead of passing json as String
yourself.
You just need to pass trigger
as the second parameter of Post.apply
as is.
Post("/tasks", trigger) ~> addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
This will require a ToEntityMarshaller[Trigger]
available in your implicit context.
And you can add it in the same way as you do in you route definition by importing/extending de.heikoseeberger.akkahttpargonaut.ArgonautSupport
and argonaut CodecJson[Trigger]
if you use argonaut for example.
In case you want to pass arbitrary string value, do
Post("/tasks").withEntity(ContentTypes.`application/json`, someJsonAsString) ~> addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
This works but it will always send the request body with a proper json. I want to test the case where the json is invalid as well.
– Niro
Jan 3 at 10:16
You need to clearly state this in your question
– Ivan Stanislavciuc
Jan 3 at 10:20
@Niro check the edit
– Ivan Stanislavciuc
Jan 3 at 10:24
add a comment |
Let akka-http create RequestEntity
on its own instead of passing json as String
yourself.
You just need to pass trigger
as the second parameter of Post.apply
as is.
Post("/tasks", trigger) ~> addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
This will require a ToEntityMarshaller[Trigger]
available in your implicit context.
And you can add it in the same way as you do in you route definition by importing/extending de.heikoseeberger.akkahttpargonaut.ArgonautSupport
and argonaut CodecJson[Trigger]
if you use argonaut for example.
In case you want to pass arbitrary string value, do
Post("/tasks").withEntity(ContentTypes.`application/json`, someJsonAsString) ~> addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
Let akka-http create RequestEntity
on its own instead of passing json as String
yourself.
You just need to pass trigger
as the second parameter of Post.apply
as is.
Post("/tasks", trigger) ~> addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
This will require a ToEntityMarshaller[Trigger]
available in your implicit context.
And you can add it in the same way as you do in you route definition by importing/extending de.heikoseeberger.akkahttpargonaut.ArgonautSupport
and argonaut CodecJson[Trigger]
if you use argonaut for example.
In case you want to pass arbitrary string value, do
Post("/tasks").withEntity(ContentTypes.`application/json`, someJsonAsString) ~> addCredentials(OAuth2BearerToken(VALID_TOKEN)) ~> Route.seal(route) ~> check {
status shouldBe StatusCodes.OK
}
edited Jan 3 at 10:23
answered Jan 3 at 8:16
Ivan StanislavciucIvan Stanislavciuc
1,670411
1,670411
This works but it will always send the request body with a proper json. I want to test the case where the json is invalid as well.
– Niro
Jan 3 at 10:16
You need to clearly state this in your question
– Ivan Stanislavciuc
Jan 3 at 10:20
@Niro check the edit
– Ivan Stanislavciuc
Jan 3 at 10:24
add a comment |
This works but it will always send the request body with a proper json. I want to test the case where the json is invalid as well.
– Niro
Jan 3 at 10:16
You need to clearly state this in your question
– Ivan Stanislavciuc
Jan 3 at 10:20
@Niro check the edit
– Ivan Stanislavciuc
Jan 3 at 10:24
This works but it will always send the request body with a proper json. I want to test the case where the json is invalid as well.
– Niro
Jan 3 at 10:16
This works but it will always send the request body with a proper json. I want to test the case where the json is invalid as well.
– Niro
Jan 3 at 10:16
You need to clearly state this in your question
– Ivan Stanislavciuc
Jan 3 at 10:20
You need to clearly state this in your question
– Ivan Stanislavciuc
Jan 3 at 10:20
@Niro check the edit
– Ivan Stanislavciuc
Jan 3 at 10:24
@Niro check the edit
– Ivan Stanislavciuc
Jan 3 at 10:24
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%2f54015198%2fakka-http-add-header-to-post-request-with-body%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