
Announcements
I have an issue with attempting to fix a Custom Connector response where the response expected is an image/png but when added to powerapps is returning as boolean.
How the Custom Connector returns in Powerapps Studio
The response in Custom Connector Edit mode
Attempting to follow this reply to import response from sample is not helpful: Solved: Re: How do I bind my custom API to a dropdown cont... - Power Platform Community (microsoft.com)
As I'm not able to copy the image content that is returned in this response.
What can I do here to fix the default response so that I can render the image in Powerapps?
Hello,
I had the same problem with a text response, and then with an image as well. I fixed it by converting the response in both cases by json with the custom code.
However, it took me a long time to make it work, because the custom code at first wouldn't execute. The response was the same than without the code, even if it was activated and everything. With the text it didn't work one day, and the day right after it worked, when I didn't change anything. For the image, the code didn't work for three days, even if it worked fine out of PowerApps. I abandoned it and came back to it a month later and now it works. So maybe it is because that function is in preliminary version or something, but it is strange, it is like PowerApps doesn't really update the connector even if it says it does.
So, I don't know where that problem comes from or how to resolve it, or if I didn’t do something more to force it to update than the button.
Anyway, it works for me now so I will share what I have:
I have added code that convert the image to base64 image to put it in a json object:
public class script: ScriptBase
{
public override async Task < HttpResponseMessage > ExecuteAsync()
{
Console.WriteLine (this.Context.OperationId);
// Check which operation ID was used
if (this.Context.OperationId == "getimage")
{
return await this.getimageAndTransformOperation().ConfigureAwait(false);
}
// Handle an invalid operation ID
HttpResponseMessage response = new HttpResponseMessage(
HttpStatusCode.BadRequest
);
response.Content = CreateJsonContent(
$"Unknown operation ID '{this.Context.OperationId}'"
);
return response;
}
private async Task < HttpResponseMessage > getimageAndTransformOperation()
{
// Use the context to forward/send an HTTP request
HttpResponseMessage response = await this.Context.SendAsync(
this.Context.Request,
this.CancellationToken
).ConfigureAwait(continueOnCapturedContext: false);
// Do the transformation if the response was successful
if (response.IsSuccessStatusCode)
{
var responseString = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(
continueOnCapturedContext: false
);
string base64Image = Convert.ToBase64String(responseString);
var str = "{ \"image_base64\": \""+base64Image+"\"}";
var str_json = JObject.Parse(str);
response.Content = CreateJsonContent(str_json.ToString());
}
return response;
}
}This second code also works and do the same thing:
private async Task < HttpResponseMessage > getimageAndTransformOperation()
{
// Use the context to forward/send an HTTP request
HttpResponseMessage response = await this.Context.SendAsync(
this.Context.Request,
this.CancellationToken
).ConfigureAwait(continueOnCapturedContext: false);
// Do the transformation if the response was successful
if (response.IsSuccessStatusCode)
{
var responseString = await response.Content.ReadAsStreamAsync().ConfigureAwait(
continueOnCapturedContext: false
);
byte[] buffer = new byte[16*1024];
MemoryStream ms = new MemoryStream();
int read;
while ((read = responseString.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
byte[] imageArray = ms.ToArray();
string base64Image = Convert.ToBase64String(imageArray);
var str = "{ \"image_base64\": \""+base64Image+"\"}";
var str_json = JObject.Parse(str);
response.Content = CreateJsonContent(str_json.ToString());
}
return response;
}When I test it (once the code executes, when it did not I just got the same as before I inserted code) I have the following response:
I copy the body of this response in the definition of the function, in the default field or import from example:
And then in Power Apps, once all that is done, I import the custom connector again and instead of true and false, I have the json field proposed:
I can then display it in an image by setting the image field of an image to:
"data:image/jpg;base64,"&imageValue2
(it can then also be stored in an excel file on OneDrive if a field of the collection is patch with this same formula, it will store the image file next to the excel file and put its path in the field, like explained in this link: Uploading images to your OneDrive with PowerApps · Elio Struyf
So now it works for me but if anyone knows why it didn’t before and then worked an undefined time later without any changes, I’m curious to know because I find it really strange. I hope it helps anyway.