Summary
For a project, I wrote a new Power Platform Custom Connector for an API call which returned the values in the response header. I wrote the connector with the response header values mapped but the Power Apps did not see those values. I always got the blank values in PowerApps. I used the Power Apps Monitor tool, and it showed the network Response did pass the header values on the Network.
After searching and asking experts I found that the Power Apps designer does not work to get the response headers. So I used the ScriptBase code for the connector to read the response header values and add to the response body with other response data.
NOTE: This post is an advanced-level step so please follow the following articles before you follow the tips from here.
Get started with custom connectors in Power Automate
Steps to resolve
Create the custom connector with the above steps or get any existing custom connector for which you need to read the response header.
To get familiar with writing the ScriptBase code follow this article. Once you have done all of the above steps you can copy the code from below in the ScriptBase.
public class Script : ScriptBase
{
public override async Task<HttpResponseMessage> ExecuteAsync()
{
if (this.Context.OperationId == "TM_CreateJob" ||
this.Context.OperationId == "OP_CreateJob")
{
// forward the request
HttpResponseMessage response = await this.Context.SendAsync(this.Context.Request, this.CancellationToken)
.ConfigureAwait(continueOnCapturedContext: false);
// get the response
var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
// parse the response to Jobject
// create new body
var headers = response.Headers;
if (response.IsSuccessStatusCode)
{
var xPag = "Not Found";
if (headers.Contains("Operation-Location"))
{
var xHeader = headers.GetValues("Operation-Location").FirstOrDefault();
xPag = xHeader.ToString();
}
var _newBody = new JObject
{
["jobId"] = xPag
};
response.Content = CreateJsonContent(_newBody.ToString());
}
return response;
}
else
{
var response = await this.Context.SendAsync(this.Context.Request, this.CancellationToken).ConfigureAwait(false);
return response;
}
} // End of ExecuteAsync
} // end of class Script
Conclusion
I hope this code is useful to you. I have found some of the following articles useful.
Create custom connector for own API | https://benediktbergmann.eu/2021/12/30/create-custom-connector-for-own-api/ |
Make Your First Custom Connector For Power Automate And Power Apps | https://www.matthewdevaney.com/make-your-first-custom-connector-for-power-automate-and-power-apps/ |
Custom Connector + Custom Headers? – Power CAT Live | https://www.youtube.com/watch?v=oVR3dFpepYc |
Transform A Custom Connector Response With C# Code | https://www.matthewdevaney.com/transform-a-custom-connector-response-with-c-code/ |