简体   繁体   中英

Can't perform HTTP Post Action from Logic App to Asp.net Core Web API

I've built many Logic Apps. I've also integrated with the Logic App API. For some reason, a Post request to an Asp.net Core Web API won't work. It works in Postman, but I can't get Logic Apps to complete the request.

The request arrives at my Web API. I can step through it during a remote debug session. I'm using the [FromBody] decorator on the API method. All the string values in the object are null.

Logic App Headers

Accept = "application/json"

ContentType = "application/json"

ContentLength = "35"

Host = "****.centralus.logic.azure.com"

API method

[HttpPost]
[Route("CreateSomething")]
public async Task<IActionResult> CreateSomething([FromBody] MyObject object)
{
  //Create something great
}

I think it might have something to do with the Headers. I noticed that the Postman request won't succeed unless I check the Host and Content-Length box in the Headers section. According to this article, Logic Apps ignores those Headers.

https://learn.microsoft.com/en-us/azure/connectors/connectors-native-http

I've built the HTTP Post Action using the API as well as configured it manually using the Logic App UI in Azure.

By the way, does anyone know the Expression that will automatically calculate the ContentLength?

UPDATE:

I finally figured this out. I had to do some Ninja coding crap to make this work. I'll post my solution tomorrow.

Does anyone know how to make this work? Thanks in advance!

When you use the Logic App API to programmatically create Logic Apps, you have to specify the Body class for when you do something like an HTTP Post. When the Body JSON displayed in the designer, it contained a single object with the objects properties. My API method could not handle this. The key was to simply post the properties in the JSON Body. To make matters worse, I'm doing two HTTP Posts in this particular Logic App. When I tried to add my object properties to the existing Body class, it caused my other HTTP Post to stop working. To overcome this, I had to create a Body2 class with the objects properties. I then had to use the following line of code to replace body2 with body before adding the JSON to the Logic App API call.

This did not work.

body = new Body()
   { 
     object = new Object()
       { 
         //Properties
       }
   } 

This worked.

body2 = new Body2()
      {
         Type = 0,
         Description = "@{items('For_each_2')?['day']?['description']}",
         Locations = item.Locations,
         Cold = "@{items('For_each_2')?['temperature']?['cold']?['value']}",
         Hot = "@{items('For_each_2')?['temperature']?['hot']?['value']}",
         Hide = 0
      }

Notice I used Replace on body2.

var options = new JsonSerializerOptions { WriteIndented = true, IgnoreNullValues = true};
string jsonString = ReplaceFirst(JsonSerializer.Serialize(myApp, options), "schema", "$schema").Replace("_else", "else").Replace("_foreach", "foreach").Replace("body2", "body");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM