Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8750351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:51:03+00:00 2026-06-13T12:51:03+00:00

I have the following action def save() = Action(parse.json) { implicit request => request.body.asOpt[IdeaType].map

  • 0

I have the following action

def save() = Action(parse.json) { implicit request =>
  request.body.asOpt[IdeaType].map { ideatype =>
    ideatype.save.fold(
      errors => JsonBadRequest(errors),
      ideatype => Ok(toJson(ideatype))
    )
  }.getOrElse     (JsonBadRequest("Invalid type of idea entity"))
}

And I’d like to test it

The web service works ok with curl like this:

curl -X post "http://localhost:9000/api/types" 
--data "{\"name\": \"new name\", \"description\": \"new description\"}" 
--header "Content-type: application/json"

which correctly returns the new resource

{"url":"/api/types/9","id":9,"name":"new name","description":"new description"}

I’m trying to test it with

"add a new ideaType, using route POST /api/types" in {
  running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {

    val json = """{"name": "new name", "description": "new description"}"""

    val Some(result) = routeAndCall(
      FakeRequest(
        POST, 
        "/api/types",
        FakeHeaders(Map("Content-Type" -> Seq("application/json"))), 
        json
      )
    )

    status(result) must equalTo(OK)
    contentType(result) must beSome("application/json")
    val Some(ideaType) = parse(contentAsString(result)).asOpt[IdeaType]

    ideaType.name mustEqual "new name"

  }
}

But I’m getting the following error:

[error] ! add a new ideaType, using route POST /api/types
[error]     ClassCastException: java.lang.String cannot be cast to play.api.libs.json.JsValue (IdeaTypes.bak.scala:35)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:36)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:35)
[error] play.api.mvc.Action$$anon$1.apply(Action.scala:170)

I followed the advices on this question: Play 2 – Scala FakeRequest withJsonBody

Am I missing something?

—

Kim Stebel solution worked fine, but then I tried with withJsonBody, like this:

    val jsonString = """{"name": "new name", "description": "new description"}"""
    val json: JsValue = parse(jsonString)

    val Some(result) = routeAndCall(
      FakeRequest(POST, "/api/types").
      withJsonBody(json)
    )

and I get the following error:

[error] ! add a new ideaType, using route POST /api/types
[error]     ClassCastException: play.api.mvc.AnyContentAsJson cannot be cast to play.api.libs.json.JsValue (IdeaTypes.bak.scala:35)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:36)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:35)

any idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:51:04+00:00Added an answer on June 13, 2026 at 12:51 pm

    You probably need to pass in a JsValue as the body of the request. Change the line

    val json = """{"name": "new name", "description": "new description"}"""
    

    to

    val jsonString = """{"name": "new name", "description": "new description"}"""
    val json = Json.parse(jsonString)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following action: users.rb: def omniauth_create auth = request.env[omniauth.auth] user = User.from_omniauth(env[omniauth.auth])
I have the following action, appointments_for_week : class StylistsController < ApplicationController def appointments_for_week stylist
I have the following index action: class ExpensesController < ApplicationController def index() @expenses =
Okay, I have the following create action #posts_controller, nested resource under discussions def create
In my controller's create action, I have the following: def create @article = Article.find(params[:id])
I have written a function having the following signature: def action_handler(request, model): This action_handler
I have the following controller action: def create @board = Board.new(params[:board]) respond_to do |format|
I have the following create action: def create @report = Report.new(params[:report]) @file = params[:report][:data_file]
I have a form_for that operates on the following action/object: def new @student =
I have the following action methods: public ActionResult ProfileSettings() { Context con = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.