Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.6 kB
1
Indexable
Never
  procedure StorePUTapi(AZAcode: Code[20])
    var
        TempBlob: Codeunit "Temp Blob";
        Client: HttpClient;
        RequestHeaders: HttpHeaders;
        MailContentHeaders: HttpHeaders;
        MailContent: HttpContent;
        ResponseMessage: HttpResponseMessage;
        RequestMessage: HttpRequestMessage;
        JObject: JsonObject;
        ResponseStream: InStream;
        APICallResponseMessage: Text;
        recRetailSetup: Record "LSC Retail Setup";
        StatusCode: Text;
        IsSuccessful: Boolean;
        RequestUrl: Text;
        Content: HttpContent;
        HttpHeadersContent: HttpHeaders;
        postData: Text;
        Jsobject: JsonObject;
    begin
        Jsobject.Add('action_id', 1);
        Jsobject.WriteTo(postData);

        clear(APICallResponseMessage);
        Clear(RequestMessage);
        Clear(RequestHeaders);
        Clear(ResponseMessage);
        recRetailSetup.get;
        // RequestUrl += recRetailSetup."Store PUT API" + '?id=' + Format(AZAcode);
        RequestUrl := 'https://stageapi.azaonline.in/api/v1/account/add_ci_action_log?id=' + Format(AZAcode);
        //RequestUrl := 'http://teststageapi.azaonline.in/swagger-ui/api-docs?id=' + Format(AZAcode);
        RequestMessage.GetHeaders(RequestHeaders);
        RequestHeaders.Add('x-ls-token', '9906d219373f6c8f80acb5bf55d16a60');

        Content.WriteFrom(postData);
        //GET HEADERS
        Content.GetHeaders(HttpHeadersContent);
        HttpHeadersContent.Clear();
        HttpHeadersContent.Remove('Content-Type');
        HttpHeadersContent.Add('Content-Type', 'application/json');
        RequestMessage.Content := Content;
        //<<
        RequestMessage.SetRequestUri(RequestUrl);
        RequestMessage.Method('PUT');

        Clear(TempBlob);
        TempBlob.CreateInStream(ResponseStream);
        IsSuccessful := Client.Send(RequestMessage, ResponseMessage);

        if not IsSuccessful then error('An API call with the provided header has failed.') else Message('PO Api Calling Successful');

        if not ResponseMessage.IsSuccessStatusCode() then begin
            StatusCode := Format(ResponseMessage.HttpStatusCode()) + ' - ' + ResponseMessage.ReasonPhrase;
            Error('The request has failed with status code ' + StatusCode);
        end;

        if not ResponseMessage.Content().ReadAs(ResponseStream) then
            Error('The response message cannot be processed.');
        ResponseStream.Read(APICallResponseMessage);
        Message('Response:' + APICallResponseMessage);
    end;