1

我目前有2个问题。我正在尝试将 GetRequest 发送到我的网络服务。这是通过 SendNotify (phonenumber: text; template: text) 方法完成的。当我通过操作调用该方法时,我收到以下错误消息:误用的标头名称。确保请求标头与 HttpRequestMessage 一起使用,响应标头与 HttpResponseMessage 一起使用,内容标头与 HttpContent 对象一起使用。When I debug, the program stops at the following line: ContentHeaders.Add('Authorization', 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI2dsadaXdrZUZTTWpNcnlBc2s1IiwiZXhwIjoxNTk1NDA3NDgzLCJpYXQiOjE1OTQzNjgyNTR9.1tAsf-x2FEvhDMyB4dsvgVKfZMLwAHcr_OLRA8RBeiY'); 该令牌当前是硬编码的,但应该从服务设置列表中删除。谁能告诉我我做错了什么?

codeunit 2020803 "Notify SMS Interface" implements SendNotifyInterface
{
    procedure CheckInputData(NotifyEntry: Record "Notify Entry")
    begin
        NotifyEntry.Get();
    end;

    procedure SendNotify(phonenumber: text; template: text)
    var


        client: HttpClient;
        RequestMessage: HttpRequestMessage;
        RequestHeaders: HttpHeaders;
        ResponseMessage: HttpResponseMessage;
        NotifyServiceSetup: Record "Notify Service Setup";
        JsonText: Text;
        IsSuccessful: Boolean;
        SendSMSURL: text;

    begin
        NotifyServiceSetup.Get();
        SendSMSURL := NotifyServiceSetup."Service URL" + '/contacts/contacts/sms/' + 'sagos/' + 
        phonenumber + '/' + template;
        JsonText := BuildJsonText();
        InitHttpRequestContent(RequestMessage, JsonText);
        InitHttpRequestMessage(RequestMessage, SendSMSURL, 'GET');

        IsSuccessful := client.Send(RequestMessage, ResponseMessage);
        if not IsSuccessful then
            Error('Authentication failed!');

        if not ResponseMessage.IsSuccessStatusCode then begin
            Error('request was not successfully');
            exit;
        end;
    end;

    local procedure InitHttpRequestContent(var RequestMessage: HttpRequestMessage; JsonText: Text)
    var
        ContentHeaders: HttpHeaders;
        NotifyServiceSetup: Record "Notify Service Setup WMR";
        bearerToken: Text[250];
        token: Text[250];
    begin
        token := NotifyServiceSetup.GetToken(NotifyServiceSetup."Authentication Token Key");
        bearerToken := 'Bearer ' + token;
        NotifyServiceSetup.Get();
        RequestMessage.Content().Clear();
        RequestMessage.Content().WriteFrom(JsonText);

        RequestMessage.Content().GetHeaders(ContentHeaders);
        ContentHeaders.Clear();
        ContentHeaders.Add('Content-Type', 'application/json');
        ContentHeaders.Add('Authorization', 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI2dsadaXdrZUZTTWpNcnlBc2s1IiwiZXhwIjoxNTk1NDA3NDgzLCJpYXQiOjE1OTQzNjgyNTR9.1tAsf-x2FEvhDMyB4dsvgVKfZMLwAHcr_OLRA8RBeiY');
    end;

    local procedure InitHttpRequestMessage(var RequestMessage: HttpRequestMessage; ServiceURL: Text; Method: Text)
    var
        RequestHeaders: HttpHeaders;
        NotifyServiceSetup: Record "Notify Service Setup WMR";
        token: Text[250];
        bearerToken: Text[250];

    begin
        token := NotifyServiceSetup.GetToken(NotifyServiceSetup."Authentication Token Key");
        bearerToken := 'Bearer ' + token;
        RequestMessage.GetHeaders(RequestHeaders);
        RequestHeaders.Clear();
        RequestHeaders.Add('Accept', 'application/json');
        RequestHeaders.Add('Authorization', 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI2dXdrZUZTTWpNcnlBc2s1IiwiZXhwIjoxNTk1NDA3NDgzLCJpYXQiOjE1OTQzNjgyNTR9.1tAsf-x2FEvhDMyB4dsvgVKfZMLwAHcr_OLRA8RBeiY');
        RequestMessage.Method(Method);
        RequestMessage.SetRequestUri(ServiceURL);
    end;

    local procedure BuildJsonText() ContentText: Text
    var
        ContentJson: JsonObject;
    begin
        ContentJson.WriteTo(ContentText);
    end;
}

我遇到的第二个问题是检查 CheckInputData (NotifyEntry: Record "Notify Entry") 方法中的数据。通知条目中有一个名为 Contact No. 的字段,其中包含有关客户的所有信息。我想在方法中询问电话号码和称呼。不幸的是,我不知道我是如何摆脱联系号码的。可以请求数据。有人能给点小费吗?

更新 1

codeunit 2020808 Test
{
    procedure sendNotify(phonenumber: text; template: text)
    var
        NotifyServiceSetup: Record "Notify Service Setup WMR";
        IsSuccessful: Boolean;
        client: HttpClient;
        content: HttpContent;
        contentHeaders: HttpHeaders;
        request: HttpRequestMessage;
        response: HttpResponseMessage;
        txtResponse: InStream;
        Url: Text;
        token: Text[250];
    begin
        NotifyServiceSetup.Get();
        token := NotifyServiceSetup.GetToken(NotifyServiceSetup."Authentication Token Key");
        content.GetHeaders(contentHeaders);
        contentHeaders.Clear();
        contentHeaders.Add('Content-Type', 'application/json');
        request.GetHeaders(contentHeaders);
        contentHeaders.Add('Authorization', StrSubstNo('Bearer %1', token));
        request.Content := content;
        Url := NotifyServiceSetup."Service URL" + '/contacts/contacts/sms/' + 'Tegos/' + phonenumber + '/' + template;

        request.SetRequestUri(Url);
        request.Method := 'GET';

        client.Send(request, response);
        response.Content().ReadAs(txtResponse);
    end;[![enter image description here][1]][1]
4

2 回答 2

2

如果问题似乎出在contentHeader.Add()这也发生在我身上。我把这段代码留给你作为参考。

procedure CheckCodeWebService(_accessToken: Text; _salesHeader: record "Sales Header")
var
    jsonRequest: Text;
    txtResponse: InStream;
    Url: Text;
    client: HttpClient;
    request: HttpRequestMessage;
    response: HttpResponseMessage;
    contentHeaders: HttpHeaders;
    content: HttpContent;
begin
    jsonRequest := '{' +
        '"GetPurchaseOrder": {' +
            '"value": {' +
                '"DataArea": {' +
                    '"PurchaseOrder": [' +
                        '{' +
                            '"PurchaseOrderHeader": {' +
                                '"ID": {' +
                                    '"value": ""' +
                                '},' +
                                '"DocumentReference": [' +
                                    '{' +
                                        '"ID": {' +
                                            '"value": " ' + _salesHeader.DealID + ' "' +
                                        '}' +
                                    '}' +
                                '],' +
                                '"SalesOrderReference": [' +
                                    '{' +
                                        '"ID": {' +
                                        '"value": " ' + _salesHeader.SalesOrderNroCisco + ' "' +
                                        '}' +
                                    '}' +
                                '],' +
                                '"Description": [' +
                                    '{' +
                                        '"value": "yes",' +
                                        '"typeCode": "details"' +
                                    '}' +
                                ']' +
                            '}' +
                        '}' +
                    ']' +
                '},' +
                '"ApplicationArea": {' +
                    '"CreationDateTime": "datetime",' +
                    '"BODID": {' +
                        '"value": "BoDID-test",' +
                        '"schemeVersionID": "V1"' +
                    '}' +
                '}' +
            '}' +
        '}' +
    '}';
    content.WriteFrom(jsonRequest);
    content.GetHeaders(contentHeaders);
    contentHeaders.Clear();
    contentHeaders.Add('Content-Type', 'application/json');
    request.GetHeaders(contentHeaders);
    contentHeaders.Add('Authorization', StrSubstNo('Bearer %1', _accessToken));
    contentHeaders.Add('Accept', 'application/xml');

    request.Content := content;

    Url := 'https://api.xxxx.com/xxxxx/ORDER/v2/sync/xxxxxxxx';

    request.SetRequestUri(Url);
    request.Method := 'POST';

    client.Send(request, response);
    response.Content().ReadAs(txtResponse);
于 2020-07-13T12:19:15.060 回答
0

好的问题解决了。我现在输入的是 RequestHeaders.Add ('Accept', 'application / json'),而不是内容类型。现在 GetRequest 可以工作了。

codeunit 2020808 Test
{
    procedure sendNotify(phonenumber: text; template: text)
    var
        NotifyServiceSetup: Record "Notify Service Setup WMR";
        IsSuccessful: Boolean;
        client: HttpClient;
        content: HttpContent;
        contentHeaders: HttpHeaders;
        request: HttpRequestMessage;
        response: HttpResponseMessage;
        txtResponse: InStream;
        Url: Text;
        token: Text[250];
    begin
        NotifyServiceSetup.Get();
        token := NotifyServiceSetup.GetToken(NotifyServiceSetup."Authentication Token Key");
        content.GetHeaders(contentHeaders);
        contentHeaders.Clear();
        //contentHeaders.Add('Content-Type', 'application/json');
        request.GetHeaders(contentHeaders);
        contentHeaders.Add('Authorization', StrSubstNo('Bearer %1', token));
        contentHeaders.Add('Accept', 'application/json');
        //request.Content := content;
        Url := NotifyServiceSetup."Service URL" + '/contacts/contacts/sms/' + 'Tegos/' + phonenumber + '/' + template;

        request.SetRequestUri(Url);
        request.Method := 'GET';

        client.Send(request, response);
        response.Content().ReadAs(txtResponse);
    end;
}
于 2020-07-14T07:19:04.877 回答