Invalid JSON response using CURL
Asked Answered
R

3

6

I have a problem with the play framework 2.1.1 (Java) telling me that I am sending Invalid JSON. There has been a similar /possibly the same issue for the play framework 2.x and 2.1.0, but it was supposed to be resolved in play framework 2.1.1 afaik: See Invalid JSON in Play Framework 2.1

This is what I am doing in the code, I have tried making it a bit shorter:

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import play.libs.Json;
import play.mvc.BodyParser;

@BodyParser.Of(BodyParser.Json.class)
public static Result login() {
    JsonNode json = request().body().asJson();
}

When I run:

curl -v -H "Content-Type: application/json" -X POST -d '{"email":"[email protected]","password":"test"}' http://localhost:9000/mobile/login

I get the following response:

< HTTP/1.1 400 Bad Request
< Content-Type: text/html; charset=utf-8
< Content-Length: 1917
...
<p id="detail">
    For request 'POST /mobile/login' [Invalid Json]
</p>
...

I have cleaned my project and rerun it several times. When running play about I get the following output:

[info] Loading project definition from /path/to/project
[info] Set current project to FFPushAlarmPlay (in build file:/path/to)
[info] This is sbt 0.12.2
[info] The current project is {file:/path/to}...
[info] The current project is built against Scala 2.10.0
[info] Available Plugins: play.Project, sbt.PlayProject, com.typesafe.sbteclipse.plugin.EclipsePlugin, com.typesafe.sbtidea.SbtIdeaPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.9.2

Am I doing something awfully wrong? Any help would be greatly appreciated as this is my first Play 2 project!

Update: I forgot to mention that I also have an iOS client which automagically builds the JSON using AFNetworking and I also get a Invalid JSON response there. So it doesn't seem to be invalid JSON causing this...

Rallentando answered 11/5, 2013 at 21:9 Comment(0)
R
0

Alright, I finally got back to looking into this and I found the answer. The problem for me was that there were people with similar issues in Play <2.1.1 and my belief was this was a similar issue, which it wasn't.

I added an unmanaged dependency to the project in the lib directory of my play-project. The library was notnoop java-apns (https://github.com/notnoop/java-apns). Internally, it uses the org.codehaus.jackson-classes which the Play framework also uses. I used version 0.1.5 which is fairly old.

The fix, just remove the apns-library from the lib-directory, remove it from the build-path in eclipse and then in project/target/Build.scala add a managed dependency:

"com.notnoop.apns" % "apns" % "0.2.3"

Hope this helps whoever runs into similar issues!

Rallentando answered 14/7, 2013 at 21:33 Comment(0)
A
17

There is nothing wrong in your code. Are you using windows command line (CMD.exe) to run curl? if you are using CMD.exe to run curl, you must fix the JSON format data you want to passed. I think in Windows, using quote is a bit different from UNIX machine.

The simplest fix is to avoid using single-quotes and use double quotes to start and end JSON data, with the double-quotes in the JSON data escaped (by \ character), may be a bit tiring but it should works:

curl -v -H "Content-Type: application/json" -X POST -d "{\"email\":\"[email protected]\",\"password\":\"test\"}" http://localhost:9000/mobile/login

or, you can use cygwin or other-UNIX-like-command-line for windows as alternative command line because its behavior likely as UNIX machine.

Hope it is useful for you friend. :)

Arsenopyrite answered 13/5, 2013 at 2:31 Comment(3)
Thanks for the reply! I am using OS X. I just tried your hint about escaping the JSON, but still get "Invalid JSON" as a response. It would have surprised me as I have an iOS client which also gets the same response and uses the AFNetworking lib for the JSON request. I forgot to mention this in my original post, sorry!Rallentando
You can also install Git which includes Git Bash with curl command.Unsung
That "remove single quotes" and "add double quotes" worked like a champ...Thanks...Doorpost
R
0

Alright, I finally got back to looking into this and I found the answer. The problem for me was that there were people with similar issues in Play <2.1.1 and my belief was this was a similar issue, which it wasn't.

I added an unmanaged dependency to the project in the lib directory of my play-project. The library was notnoop java-apns (https://github.com/notnoop/java-apns). Internally, it uses the org.codehaus.jackson-classes which the Play framework also uses. I used version 0.1.5 which is fairly old.

The fix, just remove the apns-library from the lib-directory, remove it from the build-path in eclipse and then in project/target/Build.scala add a managed dependency:

"com.notnoop.apns" % "apns" % "0.2.3"

Hope this helps whoever runs into similar issues!

Rallentando answered 14/7, 2013 at 21:33 Comment(0)
S
-1

I had the same problem in the CMD and this solved the problem:

C:\Windows\System32>curl -v -u login:password -X PUT -d "{\"test\":\"aaa\",\"name\":\"Kowalski\"}" http://127.0.0.1:5984/albums/2340234802938424f7w783

P.S. In the program Cygwin this problem does not occur.

Solemn answered 14/5, 2015 at 19:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.