Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid JSON response using CURL

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...

like image 283
mactunes Avatar asked May 11 '13 21:05

mactunes


People also ask

What causes invalid JSON response?

It means that the editor failed to get a response to the server or the response wasn't in a valid JSON format. Basically, if the editor can't communicate with the server, it will show this error message instead. To fix the problem, you essentially need to fix whatever is getting in the way of the communication.

Does Curl use JSON?

by David Callaghan on January 20th, 2022 | ~ 3 minute read. cURL is frequently used by developers working with REST API's to send and receive data using JSON notation.

What is curl in JSON?

The curl “cockpit” is yet again extended with a new command line option: --json . The 245th command line option. curl is a generic transfer tool for sending and receiving data across networks and it is completely agnostic as to what it transfers or even why.


1 Answers

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. :)

like image 128
Wayan Wiprayoga Avatar answered Sep 28 '22 06:09

Wayan Wiprayoga