Hi developers.
I don't know how to process incoming parameters sent from client with post request.
I always receive this error: x parameter is missing.
suppose i have this exposed function:
function blog_save($username, $title, $text, $excerpt = "", $tags = "blog" , $access = ACCESS_PUBLIC) { $user = get_user_by_username($username); if (!$user) { throw new InvalidParameterException('registration:usernamenotvalid'); } $obj = new ElggObject(); $obj->subtype = "blog"; $obj->owner_guid = $user->guid; $obj->access_id = strip_tags($access); $obj->method = "api"; $obj->description = strip_tags($text); $obj->title = elgg_substr(strip_tags($title), 0, 140); $obj->status = 'published'; $obj->comments_on = 'On'; $obj->excerpt = strip_tags($excerpt); $obj->tags = strip_tags($tags); $guid = $obj->save(); add_to_river('river/object/blog/create', 'create', $user->guid, $obj->guid ); $return['success'] = true; $return['message'] = elgg_echo('blog:message:saved'); return $return; } elgg_ws_expose_function('blog.save_post', "blog_save", array('username' => array ('type' => 'string', 'required' => true), 'title' => array ('type' => 'string', 'required' => true), 'text' => array ('type' => 'string', 'required' => true), 'excerpt' => array ('type' => 'string', 'required' => false), 'tags' => array ('type' => 'string', 'required' => false), 'access' => array ('type' => 'string', 'required' => false), ), "Post a blog post", 'POST', false, false);
I have written a simple client code using java. here is my code:
JSONObject object = new JSONObject(); String userName = "manatech"; object.put("username", userName); String blogTitle="Test title"; object.put("title", blogTitle); String text = "This is blog body for testing"; object.put("text", text); String excerpt = "This is some excerpt"; object.put("excerpt", excerpt); String tags = "some_tag"; object.put("tags", tags); String access = "ACCESS_PUBLIC"; object.put("access", access); CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://localhost/elgg/services/api/rest/json?method=blog.save_post"); StringEntity entity = new StringEntity(object.toJSONString()); httpPost.setEntity(entity); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); CloseableHttpResponse response = client.execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) System.out.println("Execution success"); //Code to print out the result are scaped client.close();
I always receive this result
{"status":-1,"message":"Missing parameter username in method blog.save_post"}
I think parameters sent from client did not associate with parameters on exposed function blog.save_post.
Please help
info@elgg.org
Security issues should be reported to security@elgg.org!
©2014 the Elgg Foundation
Elgg is a registered trademark of Thematic Networks.
Cover image by RaĆ¼l Utrera is used under Creative Commons license.
Icons by Flaticon and FontAwesome.
- Azartech@azar.developer
Azartech - 0 likes
- Azartech@azar.developer
Azartech - 1 like
You must log in to post replies.I mean how to pass data from client side to the server. Should I use JSON, XML, or something else? It seems the exposed function is not able to process JSON data.
I found the solution. The web service returns requested data in the form of JSON but it can not process JSON data sent from client side. Instead we should pass URL encoded data to the server.