Invalid JSON response using CURL

I have a problem with the 2.1.1 playback platform (Java) telling me that I am sending Invalid JSON. A similar problem and possibly the same problem for the 2.x and 2.1.0 platforms, but it should have been resolved in the game infrastructure 2.1.1 afaik: see Invalid JSON in the Play Framework 2.1

This is what I do in the code, I tried to make it a little 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":" test@test.de ","password":"test"}' http://localhost:9000/mobile/login 

I get the following answer:

 < 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 cleaned my project and repeated it several times. When I start playback, 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 terribly 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 that automatically creates JSON using AFNetworking, and I also get an invalid JSON response there. So this is not like invalid JSON calling this ...

+4
source share
3 answers

Well, I finally returned to the consideration of this question, and I found the answer. The problem for me was that there were people with similar problems in Play <2.1.1, and I believed that it was a similar problem, which was not.

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

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

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

Hope this helps those facing similar problems!

0
source

There is nothing wrong with the code. Do you use the window command line ( CMD.exe ) to start curl ? if you use CMD.exe to run curl , you must fix the JSON format data you want to transfer. I think that on Windows, using a quote is a little different than a UNIX machine.

The simplest fix is ​​to avoid using single quotes and using double quotes to start and end JSON data with double quotes in JSON escaped data (by the \ sign) may be a little tedious, but it should work:

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

or you can use cygwin or another UNIX-like command prompt for windows as an alternative command line, because its behavior is likely like a UNIX machine.

Hope this is helpful to you, friend. :)

+9
source

I had the same problem in 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 

PS In Cygwin, this problem does not occur.

-1
source

Source: https://habr.com/ru/post/1480285/


All Articles