首页 > 解决方案 > 文档中“为计算器服务创建客户端”的代码不起作用

问题描述

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

进口芭蕾舞女演员/http;进口芭蕾舞女演员/io;进口芭蕾舞女演员/原木;

端点 http:Client clientEndpoint { url: " http://localhost:9090 " };

功能主(字符串...参数){

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 listener { port:9090 };

// 计算器 REST 服务 @http:ServiceConfig { basePath: "/calculator" } servicehttp:Service Calculator bind listener {

// 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);
} }

谁能帮我理解我做错了什么。先感谢您!

标签: ballerina

解决方案


主函数中的 HTTP 客户端 POST 调用必须更改如下。

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

从 ballerina 0.975.0 版本开始,出站请求或消息对于 POST、PUT、PATCH 和 DELETE 是强制性的。因此不需要默认参数。此外,它还允许直接使用有效负载。

//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 客户端调用,请求或消息是可选的。因此,在添加请求参数时,将可默认的参数名称作为消息提及。即消息=请求

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

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

请参阅http-client处理有效负载示例以了解芭蕾舞演员 HTTP 客户端的行为并允许不同的有效负载类型


推荐阅读