Skip to content

Latest commit

 

History

History
130 lines (99 loc) · 4.39 KB

File metadata and controls

130 lines (99 loc) · 4.39 KB
title description ms.author ms.date ms.topic author ms.reviewer
HttpClient.Put(Text, HttpContent, var HttpResponseMessage) Method
Sends a PUT request to the specified URI as an asynchronous operation.
solsen
05/14/2024
reference
SusanneWindfeldPedersen
solsen

HttpClient.Put(Text, HttpContent, var HttpResponseMessage) Method

Version: Available or changed with runtime version 1.0.

Sends a PUT request to the specified URI as an asynchronous operation.

Syntax

[Ok := ]  HttpClient.Put(Path: Text, Content: HttpContent, var Response: HttpResponseMessage)

Parameters

HttpClient
 Type: HttpClient
An instance of the HttpClient data type.

Path
 Type: Text
The path the request is sent to.

Content
 Type: HttpContent
The HTTP request content sent to the server.

Response
 Type: HttpResponseMessage
The response received from the remote endpoint.

Return Value

[Optional] Ok
 Type: Boolean
Accessing the HttpContent property of HttpResponseMessage in a case when the request fails will result in an error. If you omit this optional return value and the operation does not execute successfully, a runtime error will occur.

Ways that HttpClient.Put calls can fail

The method HttpClient.Put can fail and return false in the following ways:

[!INCLUDEhttpclientFailureReasonsList]

Example

The PUT request method either replaces an existing resource or creates a new one using request body payload. To make an HTTP PUT request, given an HttpClient and a URI, use the HttpClient.Put method:

    local procedure PutRequest(json: Text;) ResponseText: Text
    var
        Client: HttpClient;
        Content: HttpContent
        ContentHeaders: HttpHeaders;
        IsSuccessful: Boolean;
        Response: HttpResponseMessage;
        ResponseText: Text;
    begin
        Content.GetHeaders(ContentHeaders);

        if ContentHeaders.Contains('Content-Type') then headers.Remove('Content-Type');
        ContentHeaders.Add('Content-Type', 'application/json');

        if ContentHeaders.Contains('Content-Encoding') then headers.Remove('Content-Encoding');
        ContentHeaders.Add('Content-Encoding', 'UTF8');
        
        // assume that the json parameter contains the following data
        //
        // {
        //    userId = 1,
        //    id = 1,
        //    title = "foo bar",
        //    completed = false
        // }
        Content.WriteFrom(json);
        
        IsSuccessful := Client.Put('https://jsonplaceholder.typicode.com/todos/1', Content, Response);

        if not IsSuccessful then begin
            // handle the error
        end;

        if not Response.IsSuccessStatusCode() then begin
            HttpStatusCode := response.HttpStatusCode();
            // handle the error (depending on the HTTP status code)
        end;

        Response.Content().ReadAs(ResponseText);

        // Expected output:
        //   PUT https://jsonplaceholder.typicode.com/todos/1 HTTP/1.1
        //   {
        //     "userId": 1,
        //     "id": 1,
        //     "title": "foo bar",
        //     "completed": false
        //   }
    end;

The preceding code:

  • Prepares a HttpContent instance with the JSON body of the request (MIME type of "application/json" and using the character encoding UTF8).
  • Makes a PUT request to "https://jsonplaceholder.typicode.com/todos/3".
  • Ensures that the response is successful.
  • Reads the response body as a string.

Content headers

[!INCLUDEContentHeaders]

Supported HTTP methods

[!INCLUDESupportedHTTPmethods]

See Also

Call external services with the HttpClient data type
HttpClient Data Type
Get Started with AL
Developing Extensions