0

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.

5
  • You can use python's multi-threading library to call requests.post() in another thread. Commented Aug 29, 2024 at 23:07
  • This question is similar to: Python requests call with URL using parameters. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Aug 29, 2024 at 23:07
  • @StephenNewell Isn't this question about sending the request asynchronously, not about how to encode parameters? Commented Aug 30, 2024 at 16:38
  • @Barmar - I can't honestly tell. The first sentence makes it sound like it's a parameter issue, and the second to last paragraph makes me think it's about being async. Commented Aug 30, 2024 at 18:26
  • Apologies for using some incorrect terminology. I can currently use requests with parameters to obtain some information via the API. To run a different type of API request, I need to pass some information via what I see in the documentation as client.PostAsync but I'm unsure how to do that in python. Commented Sep 3, 2024 at 0:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.