Search code examples
power-automate

Power Automate Flow - Altering the Body of the response and replace /n with /r/n


I am creating a flow on Power Automate that calls to 2 different API's, the first one, retrieves data (Body of the response), and POST it to the second API. I a, using a custom HTTP connector.

The first calls works well and I am able to grab the data of the response and send it to the second API, however, this last API is expecting the data in a specific format and doesn't like break lines (\ln), and I need to replace them with \ln\r.

I have tried so far to loop through the data and using the option "Apply to each", and a condition for replacing the values, but it always fails. (I am not sure what I am doing or what is best way of using the actions available)

This is the query I am using:

replace(replace(outputs('HTTP_call_to_the_Source')?['body'], '/n', ''), '/n', '/r/n')

The error I get is:

The execution of template action 'Apply_to_each_2' failed: the result of the evaluation of 'foreach' expression '@replace(body('HTTP_call_to_the_Source'), '/n', '/r/n')' is of type 'String'. The result must be a valid array.

No idea how to fix this and the documentation isn't helping me much.

Thanks in advance for your help.

Please see screenshot of this flow below:

enter image description here


Solution

  • First, I changed the action type, instead of apply to each, I used Compose, then I added the following function:

    replace( outputs('HTTP_call_to_the_Source')?['body'], decodeUriComponent('%0A'), decodeUriComponent('%0D%0A') )
    

    and this replaced all the \n with \r\n successfully.


    This answer was posted as an edit to the question Solved! - Power Automate Flow - Altering the Body of the response and replace /n with /r/n by the OP cacharry under CC BY-SA 4.0.