[cljs-ajax] how to post json data like CURL ?

381 views
Skip to first unread message

Bin Li

unread,
Jul 3, 2015, 8:16:39 AM7/3/15
to clojur...@googlegroups.com
I have spring contoler which talking json like :

@RequestMapping(value = "/todos/delete", method = RequestMethod.POST)
public String deleteTodo(@RequestBody Todo todo, Model model) {

logger.info("/todos/delete : todo = " + todo);
String status = "ok";
int num = 0;
try {
int id = todo.getId();
num = todoMapper.deleteByPrimaryKey(id);
} catch (Exception e) {
logger.error("/todos/delete failed : " + e);
status = "fail";
}

// result
model.addAttribute("out", "deleted " + num + " todo(s)");
model.addAttribute("status", status);
return jsonTemplate;
}

With curl , I can post it like:

curl -H "Content-Type: application/json" -X POST -d "{\"id\":1}" http://localhost:8080/todo/todos/delete

Then I can get todo initialized.

But if I post with JulianBirch/cljs-ajax :

(ajax/POST "/todo/todos/delete"
{:format :json
:response-format :json
:handler #(dispatch [:handle-del-resp %1])
:error-handler #(dispatch [:handle-error %1])
:params {"id" id}})

I am getting todo return null.

Any point will be appreciated, thanks in advanced!

Sean Johnson

unread,
Jul 4, 2015, 6:31:13 AM7/4/15
to clojur...@googlegroups.com
At the risk of promoting my own tool, I created a tool (in Clojure and ClojureScript) to debug just this sort of problem. It's called: POSThere.io ( https://github.com/SnootyMonkey/posthere.io )

Point your cURL command at POSTHere.io (you make up whatever URL you want, so I just used the same one you were using, /todo/todos/delete):

curl -H "Content-Type: application/json" -X POST -d "{\"id\":1}" http://posthere.io/todo/todos/delete

And update your CLJS code to use POSTHere.io:

(ajax/POST "http://posthere.io/todo/todos/delete"


{:format :json
:response-format :json
:handler #(dispatch [:handle-del-resp %1])
:error-handler #(dispatch [:handle-error %1])
:params {"id" id}})

Then visit that POSThere.io URL: http://posthere.io/todo/todos/delete

It'll show all the details about your two posts and you can see exactly how they differ.

Cheers,
Sean

Bin Li

unread,
Jul 6, 2015, 8:38:25 AM7/6/15
to clojur...@googlegroups.com
Thanks Sean~

http://posthere.io/ is really helped!

It turns out I have to using to transit with :json-verbose , or Spring (w/o transit) will failed to parse things like

[ "^ ", "id", 8 ]

instead of

{
"id" : 2

Reply all
Reply to author
Forward
0 new messages