文档中 "Create a Client for Calculator Service" 的代码不起作用
Code for "Create a Client for Calculator Service" in documentation not working
很抱歉问了一个非常基本的问题,我是 Ballerina 的新手,真的不知道如何进行。我在芭蕾舞演员 documentation 中复制粘贴了 "Create a Client for Calculator Service" 中的代码,即:
import ballerina/http; import ballerina/io; import ballerina/log;
endpoint http:Client clientEndpoint {
url: "http://localhost:9090" };
function 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); }
} }
然后当我 运行 在一个控制台中执行以下命令时
ballerina run calculator
和运行 另一个控制台中的以下命令
ballerina run client.bal
我收到以下错误消息:
error: ./client.bal:17:20: not enough arguments in call to 'post()'
compilation contains errors
下面显示的是示例服务代码
import ballerina/http;
endpoint http:Listener listener {
port:9090 };
// Calculator REST service @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);
} }
任何人都可以帮助我理解我做错了什么。提前致谢!
HTTP 客户端 POST main 函数中的调用必须更改如下。
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 客户端调用,请求或消息是可选的。所以在添加请求参数的时候,把默认的参数名写成message。即消息=请求
//Request as message
http:Request req = new;
response = check clientEP->get("/test", message = req);
//Without any payload
response = check clientEP->get("/test");
请参考 http-client and Handling payload 示例以了解 ballerina HTTP 客户端的行为并允许不同的负载类型
很抱歉问了一个非常基本的问题,我是 Ballerina 的新手,真的不知道如何进行。我在芭蕾舞演员 documentation 中复制粘贴了 "Create a Client for Calculator Service" 中的代码,即:
import ballerina/http; import ballerina/io; import ballerina/log;
endpoint http:Client clientEndpoint { url: "http://localhost:9090" };
function 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); } } }
然后当我 运行 在一个控制台中执行以下命令时
ballerina run calculator
和运行 另一个控制台中的以下命令
ballerina run client.bal
我收到以下错误消息:
error: ./client.bal:17:20: not enough arguments in call to 'post()' compilation contains errors
下面显示的是示例服务代码
import ballerina/http;
endpoint http:Listener listener { port:9090 };
// Calculator REST service @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); } }
任何人都可以帮助我理解我做错了什么。提前致谢!
HTTP 客户端 POST main 函数中的调用必须更改如下。
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 客户端调用,请求或消息是可选的。所以在添加请求参数的时候,把默认的参数名写成message。即消息=请求
//Request as message
http:Request req = new;
response = check clientEP->get("/test", message = req);
//Without any payload
response = check clientEP->get("/test");
请参考 http-client and Handling payload 示例以了解 ballerina HTTP 客户端的行为并允许不同的负载类型