SeaDude
SeaDude

Reputation: 4375

PowerApps Custom Connector: How to POST binary data in request body?

Using Postman to test an API, I'm able to select Body/binary then choose an image file stored locally. Postman inserts binary data somehow into the request and I get a favorable response:

enter image description here

enter image description here

When trying to build this as a Custom Connector in PowerApps, I can't find any info on what the schema for the Body of the request should be.

enter image description here

When I try to add a generic {"body": ""} body to the connector with a string type of binary, I receive this error:

Specified swagger has the following errors: 'Definition is not valid. Error: 'Error : paths/~1prebuilt~1analyze/post/parameters/2/schema/properties/body : A schema with type/format 'string/binary' can only be at the top level of a body or formData parameter. '

Upvotes: 0

Views: 2644

Answers (2)

Amitesh
Amitesh

Reputation: 1

Go to Code to add custom code and paste the following

public override async Task < HttpResponseMessage > ExecuteAsync()
{
    var contentAsString = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false);

    // Parse as JSON object
    var contentAsJson = JObject.Parse(contentAsString);

    // Convert contentAsJson to IEnumerable<KeyValuePair<string, string>> postData
    var postData = contentAsJson.Properties()
        .Select(p => new KeyValuePair<string, string>(p.Name, p.Value.ToString()))
        .ToList();

    // Create form-url-encoded content
    using (var content = new FormUrlEncodedContent(postData))
    {
        content.Headers.Clear();
        content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        this.Context.Request.Content = content;

        return await this.Context.SendAsync(
            this.Context.Request,
            this.CancellationToken
        ).ConfigureAwait(continueOnCapturedContext: false);
    }
}

Upvotes: 0

Maurits Bouman
Maurits Bouman

Reputation: 31

I was running into the same problem.

Body:

""

When you have clicked Import, go to the request -> body -> body and click the three dots -> Edit.

Under Body, it will say "key-body-output". Click the three dots and click Edit. Change Type to String (if not already) and change Format to Binary. Change Is required to Yes.

Update the connector and you can now use the custom connector with PowerApps by using ImageX.Image for the body property for example.

Upvotes: 3

Related Questions