web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Custom Connector Respo...
Power Apps
Unanswered

Custom Connector Response expected as image/png but returning boolean

(0) ShareShare
ReportReport
Posted on by 389

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

virakones_0-1643406443896.png


The response in Custom Connector Edit mode

virakones_1-1643406546221.png


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?

Categories:
I have the same question (0)
  • Din Profile Picture
    2 on at

    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:

    Din_0-1658920592932.png

     

     

    I copy the body of this response in the definition of the function, in the default field or import from example:

    Din_1-1658920592934.png

     

     

    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:

    Din_2-1658920592935.png

     

     

    I can then display it in an image by setting the image field of an image to:

    "data&colon;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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 765 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 343 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 272

Last 30 days Overall leaderboard