使用 wso2/soap 模块在 ballerina 中发送基本授权

Send basic authorization in ballerina using wso2/soap module

我正在使用 ballerina,我想连接 WSO2 身份服务器进行身份验证。

我无法使用 wso2/soap 添加基本授权。

有人可以举个例子吗?

   xml body = xml `<tes:insert_employee_operation xmlns:tes="http://teste.cv">
     <tes:name>{{username}}</tes:name>
     <tes:age>10</tes:age>
     <tes:ssn>25</tes:ssn>
  </tes:insert_employee_operation>`;


soap:SoapRequest soapRequest = {
    soapAction: "urn:insert_employee_operation",
    payload: body
};

io:println(soapRequest);

var details = soapClient->sendReceive("/services/EmployeeService", soapRequest);
match details {
    soap:SoapResponse soapResponse => {
        io:println(soapResponse);
         xml respostaXml = soapResponse.payload;
         json respostaJson = respostaXml.toJSON({});
         response.setJsonPayload(respostaJson);
         _=caller->respond(response);

        }
    soap:SoapError soapError => io:println(soapError);
}

code

soap:SoapRequst object 中还有更多可用字段。参见 https://central.ballerina.io/wso2/soap#SoapRequest

如果你的意思是ws-security那么可以使用如下:

soap:SoapRequest soapRequest = {
    soapAction: "urn:insert_employee_operation",
    payload: body,
    username: "foo",
    password: "bar"
};

您还可以使用 headers 字段设置肥皂信封 headers。

您可以在客户端端点配置下添加基本授权。

endpoint soap:Client soapClient {
        clientConfig: {
            url: "http://localhost:9000",
            auth: {
                scheme: http:BASIC_AUTH,
                username: "is_username",
                password: "is_password"
            }
        }
    };

这会将 Authorization header 添加到 HTTP 请求中。完整代码如下所示:

import ballerina/http;
import ballerina/io;
import ballerina/log;
import wso2/soap;

endpoint soap:Client soapClient {
    clientConfig: {
        url: "http://localhost:9000",
        auth: {
            scheme: http:BASIC_AUTH,
            username: "is_username",
            password: "is_password"
        }
    }
};

public function main(string... args) {
    xml body = xml `<tes:insert_employee_operation xmlns:tes="http://teste.cv">
                        <tes:name>{{username}}</tes:name>
                        <tes:age>10</tes:age>
                        <tes:ssn>25</tes:ssn>
                    </tes:insert_employee_operation>`;


    soap:SoapRequest soapRequest = {
        soapAction: "urn:insert_employee_operation",
        payload: body
    };

    io:println(soapRequest);

    var details = soapClient->sendReceive("/services/EmployeeService", soapRequest);
    match details {
        soap:SoapResponse soapResponse => {
            io:println(soapResponse);
            xml respostaXml = soapResponse.payload;
            json respostaJson = respostaXml.toJSON({});
            response.setJsonPayload(respostaJson);
            _ = caller->respond(response);

        }
        soap:SoapError soapError => io:println(soapError);
    }
}