How to send parameters on O365 Power Automate HTTP Request connector

488 views Asked by At

I've been troubleshooting this for days now but still no luck. I'm trying to send parameters to an API link provided by Microsoft O365 Power Automate, this API requires a customer number, company code, and posting date and in return, it will send me a table with the list of items that have the same customer number, company code, and posting date. When I'm doing testing in Postman the sends status code 200, but when using VS and my code it always returns a status code 400.

SoaController.cs

[HttpPost]
public async Task<IActionResult> Index(string company, string customer, string asof)
{
    using (var client = new HttpClient())
    {
        SoaParams soaParams = new SoaParams
        {
            Posting_Date = asof,
            Company_Code = company,   
            Customer_Number = customer 
        };

        var SoaJson = JsonConvert.SerializeObject(soaParams);   
        var buffer = Encoding.UTF8.GetBytes(SoaJson);   
        var byteContent = new ByteArrayContent(buffer); 
        byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        client.BaseAddress = new Uri(SD.ApiUri);   
        var response = await client.PostAsync(SD.ApiUri, byteContent);  

        if (response.IsSuccessStatusCode)   
        {
            return RedirectToAction(nameof(Success), Json(response)); 
        }
        else
        {
            return RedirectToAction(nameof(Failed), Json(response)); 
        }
    }
}

The below image shows that the parameters needed are correct. enter image description here

enter image description here

But it's SuccessStatusCode always returns false

1

There are 1 answers

0
Jose Baleros Jr On

I use a code provided by PostMan that look like this:

public List<BapiOpenItemDto> GetResponse(SoaParams soaParams, string uri)
        {
            var SoaJson = JsonConvert.SerializeObject(soaParams);
            var client = new RestClient(uri);
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
            request.AddHeader("Content-Type", "application/json");
            request.AddParameter("application/json", "[\r\n" + SoaJson + "\r\n]\r\n", ParameterType.RequestBody);
            IRestResponse<List<BapiOpenItemDto>> response = client.Execute<List<BapiOpenItemDto>>(request);
            return response.Data;
        }

and its working now.