[Python] Python equivalent of HttpClient PostAsync for sending an array of parameters as part...

Discussão em 'Python' iniciado por Stack, Maio 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have been unable to find how to use similar PostAsync for an enterprise API.

    This is the example from documenation in C#:

    string json = string.Empty;

    using ( HttpClient client = new HttpClient() )
    {
    string object= "UserNames";
    string method = "methodname";
    string requestUrl = $"http://server/service/object/invoke/{object}?method={method}";

    // provide token in the Authorization header
    client.DefaultRequestHeaders.TryAddWithoutValidation(
    "Authorization",
    "b/authorizationkey" );

    string[] parameters = new[] { "sa", "", "", "", "" };

    // pass the array of parameters as the request data
    string contentStr = JsonConvert.SerializeObject( parameters );

    // send the post request
    HttpResponseMessage response = client.PostAsync( requestUrl.ToString(), new StringContent( contentStr, Encoding.UTF8, "application/json" ) ).Result;

    using ( HttpContent content = response.Content )
    {
    Task<string> result = content.ReadAsStringAsync();
    json = result.Result;
    }
    }


    I've been using 'requests' package successfully for getting information from the api but am at a roadblock when trying to find an equivalent way to PostAsync.

    Continue reading...

Compartilhe esta Página