Skip to main content

Notifications

Power Apps - Building Power Apps
Unanswered

Graph API HTTP request direct in Power Apps

(1) ShareShare
ReportReport
Posted on by 179

Hi all,

 

I just watched @RezaDorrani video about interacting with Graph API within Power Apps. Link here

 

I was wondering if it can go beyond getting data and actually posting data?

 

So I am trying to add a member to a group and I thought I could use something like this:

 

 

Office365Groups.HttpRequestV2("https://graph.microsoft.com/v1.0/groups/a95c6664-3f76-434c-991d-924f8384f46a/members/$ref","POST","",{ContentType: "application/json"})

 

 

But unsure how to structure the body section for the member to add.

 

da1vea_0-1709703278694.png

 

I thought the body might have contained something like this:

 

 

{"@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/b34d7ee3-0358-4923-a202-da5e092941a0"}

 

 

But unsure how to structure the string. Is anyone able to p9ojnt me in the right direction?

 

Thanks,
David

  • Suggested answer
    OwenS28 Profile Picture
    OwenS28 47 on at
    Graph API HTTP request direct in Power Apps
    @da1vea Hey thanks for getting back. I've been digging all day and finally got it to work. (The error being:  "code": "BadRequest", "message": "Empty Payload. JSON content expected." )  All thanks to Reza, I wish I found his video sooner. I thought i'd post the solution so others can see it in the future. Here is the link: Power Apps: 🔍 Search SharePoint Lists without Delegation Issues 💪 . From like 11 min to 15 min is specifically the solution but the whole video is good for context. Essentially you need to encode the json formatted text into base 64. There isn't a built in function to do that but someone has made a powerfx With function to encode it. I will include it below. (I modified it to fix the errors) Just copy paste it into your button before the http request. Then modify the varRequestBody variable to be the JSON you need and insert varFile into that third parameter of the Http Request. Hope this helps someone!
    Set(varRequestBody,
        "{
            ""exampleColumn"": ""exampleData"",
            ""exampleColumn2"": ""exampleData2""
        }"
    );
    
    Set(varFile, "data:text/plain;base64," &
    With({
        InputText: varRequestBody,
        AsciiTable:AddColumns(Sequence(255,1),char,Char(Value)),
        B64ToBin:
        Table(
            {b64:"A",bin:"000000"},
            {b64:"B",bin:"000001"},
            {b64:"C",bin:"000010"},
            {b64:"D",bin:"000011"},
            {b64:"E",bin:"000100"},
            {b64:"F",bin:"000101"},
            {b64:"G",bin:"000110"},
            {b64:"H",bin:"000111"},
            {b64:"I",bin:"001000"},
            {b64:"J",bin:"001001"},
            {b64:"K",bin:"001010"},
            {b64:"L",bin:"001011"},
            {b64:"M",bin:"001100"},
            {b64:"N",bin:"001101"},
            {b64:"O",bin:"001110"},
            {b64:"P",bin:"001111"},
            {b64:"Q",bin:"010000"},
            {b64:"R",bin:"010001"},
            {b64:"S",bin:"010010"},
            {b64:"T",bin:"010011"},
            {b64:"U",bin:"010100"},
            {b64:"V",bin:"010101"},
            {b64:"W",bin:"010110"},
            {b64:"X",bin:"010111"},
            {b64:"Y",bin:"011000"},
            {b64:"Z",bin:"011001"},
            {b64:"a",bin:"011010"},
            {b64:"b",bin:"011011"},
            {b64:"c",bin:"011100"},
            {b64:"d",bin:"011101"},
            {b64:"e",bin:"011110"},
            {b64:"f",bin:"011111"},
            {b64:"g",bin:"100000"},
            {b64:"h",bin:"100001"},
            {b64:"i",bin:"100010"},
            {b64:"j",bin:"100011"},
            {b64:"k",bin:"100100"},
            {b64:"l",bin:"100101"},
            {b64:"m",bin:"100110"},
            {b64:"n",bin:"100111"},
            {b64:"o",bin:"101000"},
            {b64:"p",bin:"101001"},
            {b64:"q",bin:"101010"},
            {b64:"r",bin:"101011"},
            {b64:"s",bin:"101100"},
            {b64:"t",bin:"101101"},
            {b64:"u",bin:"101110"},
            {b64:"v",bin:"101111"},
            {b64:"w",bin:"110000"},
            {b64:"x",bin:"110001"},
            {b64:"y",bin:"110010"},
            {b64:"z",bin:"110011"},
            {b64:"0",bin:"110100"},
            {b64:"1",bin:"110101"},
            {b64:"2",bin:"110110"},
            {b64:"3",bin:"110111"},
            {b64:"4",bin:"111000"},
            {b64:"5",bin:"111001"},
            {b64:"6",bin:"111010"},
            {b64:"7",bin:"111011"},
            {b64:"8",bin:"111100"},
            {b64:"9",bin:"111101"},
            {b64:"+",bin:"111110"},
            {b64:"/",bin:"111111"}
        )},
        With({
        BinRep:
        Concat(
            AddColumns(ForAll(Split(InputText,""), {Result: ThisRecord.Value}),dec,LookUp(AsciiTable,char=Result).Value),//Convert text to Ascii character code (decimal)
            Concat(Sequence(8,8,-1),Text(If(And(Mod(dec,Power(2,Value))>=Power(2,Value-1),Mod(dec,Power(2,Value))<Power(2,Value)),1,0)))&"","")//Convert decimal to binary
        },
            With({b64string:Concat(
                Sequence(
                    RoundUp(Len(BinRep)/6,0),0),
                    LookUp(
                        B64ToBin,
                        bin=Mid(BinRep&Left("000000",6-Mod(Len(BinRep),6)),6*Value+1,6) //Left("000000"....) is padding right with zero
                    ).b64&"", 
                    ""
                )},
                b64string&Left("====",Mod(4-Mod(Len(b64string),4),4))//Convert binary to base64
            )
        )
    ));
    
    Office365Groups.HttpRequestV2(
        {your graph api here},
        {your method here},
        varFile
    )
     
  • da1vea Profile Picture
    da1vea 179 on at
    Graph API HTTP request direct in Power Apps
    @OwenS28 - I didnt. Its been a while now so im not even sure where i was trying to do this 
  • OwenS28 Profile Picture
    OwenS28 47 on at
    Graph API HTTP request direct in Power Apps
    @da1vea Did you ever figure this out? I'm having the same problem trying to update a files properties which are in a sharepoint doc library. I can do it in the graph explorer and power automate, but no matter what I do in power apps, it says Empty Payload. JSON content expected.
  • da1vea Profile Picture
    da1vea 179 on at
    Re: Graph API HTTP request direct in Power Apps

    Hi @DJ_Jamba ,

     

    PowerApps doesn't like that metadata bit

  • DJ_Jamba Profile Picture
    DJ_Jamba 2,327 on at
    Re: Graph API HTTP request direct in Power Apps

    In an http post doing the same thing in Power Automate, it’s:

     

    Headers

    accept: application/json;odata=verbose

    content-type: application/json;odata=verbose

     

    Body

    {

      "__metadata": {

        "type":"SP.User"

      },

      "LoginName":"i:0#.f|membership|YOUR-EMAIL-ADDRESS"

    }

     

    But that’s for a permissions group in a SP site. I think SP.User needs to be something else

  • da1vea Profile Picture
    da1vea 179 on at
    Re: Graph API HTTP request direct in Power Apps

    UPDATE

     

    So I managed to get PowerApps to accept this version:

     

     

    Set(
     varGroupChange,
     Office365Groups.HttpRequestV2(
     "https://graph.microsoft.com/v1.0/groups/a95c6664-3f76-434c-991d-924f8384f46a/members/$ref",
     "POST",
     JSON(
     {
     '@odata.id': "https://graph.microsoft.com/v1.0/directoryObjects/b34d7ee3-0358-4923-a202-da5e092941a0"
     }
     ),
     {
     ContentType: "application/json"
     }
     )
    )

     

     

    But when I run it, I get the error "Empty Payload. JSON content expected"

     

    da1vea_0-1709761477725.png

     

    If I look at the details:

     

     

    {
     "status": 400,
     "duration": 55.3,
     "dataSource": "Office365Groups",
     "responseSize": 301,
     "controlName": "Button1",
     "propertyName": "OnSelect",
     "nodeId": 12,
     "formulaData": {
     "script": "Set(\n varGroupChange,\n Office365Groups.HttpRequestV2(\n \"https://graph.microsoft.com/v1.0/groups/a95c6664-3f76-434c-991d-924f8384f46a/members/$ref\",\n \"POST\",\n JSON(\n {\n '@odata.id': \"https://graph.microsoft.com/v1.0/directoryObjects/b34d7ee3-0358-4923-a202-da5e092941a0\"\n }\n ),\n {\n ContentType: \"application/json\"\n }\n )\n)\n",
     "spanStart": 29,
     "spanEnd": 417
     },
     "data": {
     "context": {
     "entityName": "Button1",
     "propertyName": "OnSelect",
     "id": 112,
     "nodeId": 12,
     "diagnosticContext": {
     "span": {
     "start": 29,
     "end": 417
     },
     "dataOperation": {
     "protocol": "rest",
     "operation": "HttpRequestV2",
     "dataSource": "Office365Groups"
     },
     "formula": "Set(\n varGroupChange,\n Office365Groups.HttpRequestV2(\n \"https://graph.microsoft.com/v1.0/groups/a95c6664-3f76-434c-991d-924f8384f46a/members/$ref\",\n \"POST\",\n JSON(\n {\n '@odata.id': \"https://graph.microsoft.com/v1.0/directoryObjects/b34d7ee3-0358-4923-a202-da5e092941a0\"\n }\n ),\n {\n ContentType: \"application/json\"\n }\n )\n)\n"
     }
     },
     "request": {
     "url": "https://australia-001.azure-apim.net/apim/office365groups/ff5089fa29cd450a8cd75b2bfd8bb043/v2/httprequest",
     "method": "POST",
     "headers": {
     "Uri": "https://graph.microsoft.com/v1.0/groups/a95c6664-3f76-434c-991d-924f8384f46a/members/$ref",
     "Method": "POST",
     "ContentType": "application/json",
     "x-ms-user-agent": "PowerApps/3.24024.12 (Web AuthoringTool; AppName=<NonCloudApp>)",
     "x-ms-client-session-id": "ab9d6b7e-b776-492a-834b-289de3238fe8",
     "x-ms-client-request-id": "9b25a71a-0ba8-40d6-bbd6-565e39194add",
     "x-ms-client-environment-id": "/providers/Microsoft.PowerApps/environments/Default-49bd9903-be12-4915-8753-05826fa0759b",
     "x-ms-client-tenant-id": "49bd9903-.....",
     "x-ms-client-object-id": "dff3a88d-29bc-4860-b563-68e82e6644de",
     "Accept-Language": "en-GB",
     "Accept": "*/*",
     "Cache-Control": "no-cache, no-store",
     "x-ms-request-method": "POST",
     "x-ms-request-url": "/apim/office365groups/ff5089fa29cd450a8cd75b2bfd8bb043/v2/httprequest"
     },
     "body": ""
     },
     "response": {
     "duration": 55.3,
     "size": 301,
     "status": 400,
     "headers": {
     "client-request-id": "bf95a170-650d-4ce8-ad0e-b18e02b72762",
     "content-encoding": "gzip",
     "Content-Type": "application/json",
     "Date": "Wed, 06 Mar 2024 21:44:18 GMT",
     "Request-Id": "bf95a170-650d-4ce8-ad0e-b18e02b72762",
     "strict-transport-security": "max-age=31536000",
     "timing-allow-origin": "*",
     "vary": "Accept-Encoding",
     "x-ms-ags-diagnostic": {
     "ServerInfo": {
     "DataCenter": "Australia East",
     "Slice": "E",
     "Ring": 5,
     "ScaleUnit": "002",
     "RoleInstance": "SY2PEPF00002E3F"
     }
     },
     "x-ms-apihub-cached-response": false,
     "x-ms-apihub-obo": true,
     "x-ms-environment-id": "Default-49bd9903-......",
     "x-ms-client-request-id": "9b25a71a-0ba8-40d6-bbd6-565e39194add"
     },
     "body": {
     "error": {
     "code": "BadRequest",
     "message": "Empty Payload. JSON content expected.",
     "innerError": {
     "date": "2024-03-06T21:44:19",
     "request-id": "bf95a170-650d-4ce8-ad0e-b18e02b72762",
     "client-request-id": "bf95a170-650d-4ce8-ad0e-b18e02b72762"
     }
     }
     },
     "responseType": "text"
     },
     "startTime": 1599802,
     "name": "https://australia-001.azure-apim.net/invoke",
     "fetchStart": 1599802.4,
     "domainLookupStart": 1599802.4,
     "domainLookupEnd": 1599802.4,
     "connectStart": 1599802.4,
     "connectEnd": 1599802.4,
     "secureConnectionStart": 1599802.4,
     "requestStart": 1599804.3,
     "responseStart": 1599852.9,
     "responseEnd": 1599853.3,
     "transferSize": 497,
     "nextHopProtocol": "h2"
     }
    }

     

     

    I can see the body section looks blank ("")

     

    No issues when using Graph explorer

    da1vea_0-1709763142051.png

     

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

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,422

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,711

Leaderboard