文档中“为计算器服务创建客户端”的代码不起作用

时间:2018-07-04 20:25:25

标签: ballerina

很抱歉问了一个非常基本的问题,我是芭蕾舞女演员的新手,并不真正知道如何进行此操作。我将粘贴的代码复制到芭蕾舞女演员documentation的“为计算器服务创建客户端”中,

  

导入芭蕾舞女演员/ http;进口芭蕾舞演员/ io;导入芭蕾舞演员/日志;

     

端点http:客户端clientEndpoint {       url:“ http://localhost:9090”};

     

函数main(string ... args){

http:Request req = new;

// Set the JSON payload to the message to be sent to the endpoint.
json jsonMsg = { a: 15.6, b: 18.9, operation: "add" };
req.setJsonPayload(jsonMsg);

var response = clientEndpoint->post("/calculator/operation", request = req);
match response {
    http:Response resp => {
        var msg = resp.getJsonPayload();
        match msg {
            json jsonPayload => {
                string resultMessage = "Addition result " + jsonMsg["a"].toString() +
                    " + " + jsonMsg["b"].toString() + " : " +
                    jsonPayload["result"].toString();
                io:println(resultMessage);
            }
            error err => {
                log:printError(err.message, err = err);
            }
        }
    }
    error err => { log:printError(err.message, err = err); }
} }

然后在一个控制台中运行以下命令

  

芭蕾舞女演员运行计算器

并在另一个控制台中运行以下命令

  

芭蕾舞女演员运行client.bal

我收到以下错误消息:

  

错误:./client.bal:17:20:调用'post()'的参数不足   编译中包含错误

下面显示的是示例服务代码

  

导入芭蕾舞女演员/ http;

     

端点http:Listener监听器{       端口:9090};

     

//计算器REST服务@http:ServiceConfig {basePath:   “ / calculator”} servicehttp:服务计算器绑定侦听器{

// Resource that handles the HTTP POST requests that are directed to
// the path `/operation` to execute a given calculate operation
// Sample requests for add operation in JSON format
// `{ "a": 10, "b":  200, "operation": "add"}`
// `{ "a": 10, "b":  20.0, "operation": "+"}`

@http:ResourceConfig {
    methods: ["POST"],
    path: "/operation"
}
executeOperation(endpoint client, http:Request req) {
    json operationReq = check req.getJsonPayload();
    string operation = operationReq.operation.toString();

    any result = 0.0;
    // Pick first number for the calculate operation from the JSON request
    float a = 0;
    var input = operationReq.a;
    match input {
        int ivalue => a = ivalue;
        float fvalue => a = fvalue;
        json other => {} //error
    }

    // Pick second number for the calculate operation from the JSON request
    float b = 0;
    input = operationReq.b;
    match input {
        int ivalue => b = ivalue;
        float fvalue => b = fvalue;
        json other => {} //error
    }

    if(operation == "add" || operation == "+") {
        result = add(a, b);
    }

    // Create response message.
    json payload = { status: "Result of " + operation, result: 0.0 };
    payload["result"] = check <float>result;
    http:Response response;
    response.setJsonPayload(payload);

    // Send response to the client.
    _ = client->respond(response);
} }

谁能帮助我了解我做错了什么。预先谢谢你!

1 个答案:

答案 0 :(得分:4)

主要功能中的HTTP客户端POST调用必须进行如下更改。

var response = clientEndpoint->post("/calculator/operation", req);

从芭蕾舞女演员0.975.0版本开始,出站请求或消息对于POST,PUT,PATCH和DELETE是必需的。因此,不需要defaultable参数。此外,它还允许直接使用有效负载。

//Request as message
http:Request req = new;
response = check clientEP->post("/test", req);

//Text payload
response = check clientEP->post("/test", "Sample Text");

对于GET,HEAD和OPTIONS客户端调用,请求或消息是可选的。因此,在添加请求参数时,请提及默认参数名称作为消息。即message = req

//Request as message
http:Request req = new;
response = check clientEP->get("/test", message = req);

//Without any payload
response = check clientEP->get("/test");

请参考http-clientHandling payload示例以了解芭蕾舞女演员HTTP客户端的行为并允许使用不同的有效负载类型