oetzi86oetzi86 MemberComments: 0 Received thanks: 0 Member since: January 18

Hallo,
ich bin neu hier und habe das Forum durchsucht und passendes nicht gefunden, also erstelle ich einen neuen Thread.

Ziel: Shopware 6 und Microsoft Dynamic 365 Business Central über API verbinden.

Folgende Code habe ich bereits verwendet aber ohne erfolg kann jemand dazu noch was sagen oder tipp geben?

procedure GetToken()
    var
        IsSuccessful: Boolean;
        client: HttpClient;
        content: HttpContent;
        curcontent: HttpContent;
        contentHeaders: HttpHeaders;
        request: HttpRequestMessage;
        response: HttpResponseMessage;
        txtResponse: InStream;
        JsonObject: JsonObject;
        JsonArray: JsonArray;
        JsonToken: JsonToken;
        JsonValue: JsonValue;
        JsonText: Text;
        Url: Text;
        i: Integer;
        PayLoad: Text;
        ShopItem: Record ShopItemTable;
        ItemRec: Record Item;
        access_token: Text;
        RESTHelper: Codeunit "Web Request Helper";

    begin

        PayLoad := '{"grant_type": "client_credentials","client_id": "***********","client_secret": "*****************"}';
        content.WriteFrom(PayLoad);
        content.GetHeaders(contentHeaders);
        contentHeaders.Remove('Content-Type');
        contentHeaders.Add('Content-Type', 'application/json');
        Url := 'https://shopware6webshop.de/api/oauth/token';
        request.Content(content);
        request.SetRequestUri(Url);

        request.Method := 'POST';
        curcontent := request.Content;
        client.Post(Url, content, response);
        //client.Send(request, response);
        response.Content().ReadAs(JsonText);
        if not response.IsSuccessStatusCode then
            Message('Server schickt einen Fehler:\\' +
                  'Status code: %1\' +
                  'Description: %2\' +
                  'Message: %3',
                  response.HttpStatusCode,
                  response.ReasonPhrase, JsonText)

        else begin

            JsonToken.ReadFrom(JsonText);
            JsonObject.ReadFrom(JsonText);

            if not JsonObject.Contains('access_token') then
                Message('Token nicht gefunden!')
            else
                JsonObject.Get('access_token', JsonToken);
            JsonValue := JsonToken.AsValue();
            access_token := JsonValue.AsText();
            CallService(access_token);
            
        end;
    end;
Bisher alles gut ich bekomme access token zurück und damit kann ich mich anmelden usw.
Was nicht funktioniert bei abfrage "/api/v3/product/ " oder "api/v3/search/product" Payload irgendwie immer malformed
Fehlermeldung: {"errors":[{"code":"0","status":"400","title":"Bad Request","detail":"The JSON payload is malformed.
       
procedure CallService(access_token: Text): Text
    var
        Client: HttpClient;
        RequestHeaders: HttpHeaders;
        RequestContent: HttpContent;
        ResponseMessage: HttpResponseMessage;
        RequestMessage: HttpRequestMessage;
        ResponseText: Text;
        contentHeaders: HttpHeaders;
        PayLoad: Text;
        RequestUrl: Text;
        Json: JsonValue;
        RequestType: Enum "Http Request Type";

    begin
        
        RequestUrl := 'https://shopware6webshop.de/api/v3/search/product';
        //PayLoad := '{"includes": {"product": ["id", "name"]}}';

        //PayLoad := '"application/json",{"name" : "PostMan Test", "productNumber" : "11111155",  "tax" : {"name": "test","taxRate": 15},"stock": 15,"price" : ["currencyId" :"b7d2554b0ce847cd82f3ac9bd1c0dfca","gross": 15,"net": 10,"linked" : false}]}';
        PayLoad := 'application/json,{"includes": {"product": ["id", "name"]}}';
        //quest.AddParameter("application/json", "{\r\n    \"includes\": {\r\n        \"product\": [\"id\", \"name\"]\r\n    }\r\n}",  ParameterType.RequestBody);


        RequestHeaders := Client.DefaultRequestHeaders();
        RequestHeaders.Add('Authorization', 'Bearer ' + access_token);
        RequestType := "Http Request Type".FromInteger(2);

        case RequestType of
            RequestType::Get:
                Client.Get(RequestURL, ResponseMessage);
            RequestType::patch:
                begin

                    RequestContent.WriteFrom(PayLoad);

                    RequestContent.GetHeaders(contentHeaders);

                    contentHeaders.Remove('Content-Type');
                    //contentHeaders.Remove('Charset');
                    contentHeaders.Add('Content-Type', 'application/json');
                    //contentHeaders.Add('Charset', 'utf-8');
                    RequestMessage.Content := RequestContent;

                    RequestMessage.SetRequestUri(RequestURL);

                    RequestMessage.Method := 'POST';
                    Client.DefaultRequestHeaders.Add('User-Agent', 'Dynamics 365');
                    client.Send(RequestMessage, ResponseMessage);
                    //Client.Post(RequestURL, RequestContent, ResponseMessage);
                    RequestContent.ReadAs(ResponseText);
                end;
        end;

        ResponseMessage.Content().ReadAs(ResponseText);
        exit(ResponseText);
    end;

Ich kann mit hilfe POSTMAN alles abfragen was ich möchte.

 

Sign In or Register to comment.