简体   繁体   中英

elgg web services client

I'm coding a custom yet simple client to test elgg web services with Java. I want to send a post request to the server with a simple parameter but.

Here is my exposed function in elgg:

function hello_world($name) {
    return "Hello ".$name;
}


elgg_ws_expose_function(
    "test.echo",
    "hello_world",
    array("name" => array("type" => "string", "required" => true)),
    'A testing method which echos back a string',
    'POST',
    false,
    false
);

and here is my java code for sending a post request:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package readjson;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;


public class Main {

    public static void main(String[] args) {


        try {
            CloseableHttpClient client = HttpClients.createDefault();
            JSONObject object = new JSONObject();
            String name = "Mousa";
            object.put("name", name);
            HttpPost httpPost = new      HttpPost("http://localhost/elgg/services/api/rest/json?method=test.echo");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        StringEntity entity = new StringEntity(object.toJSONString());
        System.out.println(object);
        httpPost.setEntity(entity);
        CloseableHttpResponse response = client.execute(httpPost);
        System.out.println(response.getStatusLine().getStatusCode());
        HttpEntity httpEntity = response.getEntity();
        StringBuilder stringBuilder = new StringBuilder();
            InputStream inputStream = httpEntity.getContent();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
            inputStream.close();
            System.out.println(stringBuilder.toString());
        client.close();
        }
        catch(Exception ex) {
            System.out.println(ex.getLocalizedMessage());
        }
    }
}

But I'm receiving this output and there is a problem with post request. I don't know what is wrong with that code:

{"name":"Mousa"}

200


{"status":-1,"message":"Missing parameter name in method test.echo"}

It says that "name" parameter is missing!!!

Please help

As far as I remember, Elgg's web services can't handle JSON in request body, use serialized query string instead, eg:

name=Mousa&interest[]=interest1&interest[]=interest2

Elgg will likely use parse_str() , so this might be helpful: http://php.net/manual/en/function.parse-str.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM