Forum Discussion

bender1234's avatar
bender1234
Regular Visitor
2 years ago
Solved

Passing Cookie in Header fails when querying GraphQL backend with GraphQL variables

Hi

 

I have a JWT token and need to pass it along with my POST request to a REST endpoint like so: 

 

 

 

 

let
	url = "https://test.my-website.com/graphql",
	token = "express:sess=ey....",

    Source = Web.Contents(
	url,
	[
		Headers=[
			#"Method"="POST",
			#"Content-Type"="application/json",
			#"Cookie"=token,
		],
		Content=Text.ToBinary("{""query"": ""{  allData( $filter: [FilterInput]){  allData(filter: $filter) {
					  field1
					  field2
					  } }""}")
	]
    ),

    #"JSON" = Json.Document(Source)
in
    JSON

 

 

 

 

If I run the same in Postman and pass the token in the header with "Cookie" as name of the header entry and "express:sess=ey...." as the value, it works all fine.

When I do the same in the advanced editor of a "Blank Query" data source, I get "Expression.Error: Access to the resource is forbidden."

Looking at the Query diagnostics, the server returned: Message: The remote server returned an error: (403) Forbidden.

I followed this tutorial to send a GraphQL query: https://www.linkedin.com/pulse/connect-powerbi-graphql-endpoint-amir-khan/ 

 

How does Power Bi blank query works differently compared to Postman? Is it using a different encoding or something?

 

Any help is much appreciated.

 

Thanks

 

 

  • Thanks Gao,

     

    I managed to solve my problem and it had nothing to do with the authentication. Using a cookie like the example below works just fine.
    The actual problem was the graphql part which had incorrect formatting. I was mislead by the error message.

    If anyone wants to send a graphql request from power bi with actual variables, this is how it worked for me:

    let
    	url = "https://my-website.com/graphql",
    	token = "express:sess=ey... [replace this value with you Cookie ]",
    	body = "{ ""query"": ""query allParameter($filter: [FilterInput]) { allParameter(filter: $filter) { parameterId } }"", ""variables"": {""filter"": []} }",
        response = Web.Contents(
    	url,
    	[
    		Headers=[
    			#"Method"="POST",
    			#"Content-Type" = "application/json",
    			#"Cookie"=token
    		],
    		Content=Text.ToBinary(body)
    	]
        ),
    
        jsonResponse = Json.Document(response)
       
    in
        jsonResponse

     

2 Replies

  • bender1234's avatar
    bender1234
    Regular Visitor

    Thanks Gao,

     

    I managed to solve my problem and it had nothing to do with the authentication. Using a cookie like the example below works just fine.
    The actual problem was the graphql part which had incorrect formatting. I was mislead by the error message.

    If anyone wants to send a graphql request from power bi with actual variables, this is how it worked for me:

    let
    	url = "https://my-website.com/graphql",
    	token = "express:sess=ey... [replace this value with you Cookie ]",
    	body = "{ ""query"": ""query allParameter($filter: [FilterInput]) { allParameter(filter: $filter) { parameterId } }"", ""variables"": {""filter"": []} }",
        response = Web.Contents(
    	url,
    	[
    		Headers=[
    			#"Method"="POST",
    			#"Content-Type" = "application/json",
    			#"Cookie"=token
    		],
    		Content=Text.ToBinary(body)
    	]
        ),
    
        jsonResponse = Json.Document(response)
       
    in
        jsonResponse

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi bender1234 ,

    Ensure Correct Token Format: Double-check the format of your JWT token. Ensure it's correctly formatted and includes the necessary prefix (usually "Bearer") if required by your endpoint. For JWT tokens, the header is typically passed as an "Authorization" header rather than a "Cookie".

    Use Authorization Header: If your endpoint expects a JWT token for authentication, it's more common to use the "Authorization" header instead of "Cookie". Here's an adjusted example:

    let
      url = "https://test.my-website.com/graphql", 
      token = "Bearer ey....", 
      Source = Web.Contents(
        url, 
        [
          Headers = [#"Method" = "POST", #"Content-Type" = "application/json", #"Authorization" = token], 
          Content = Text.ToBinary(
            "{""query"": ""{  allData( $filter: [FilterInput]){  allData(filter: $filter) {
                          field1
                          field2
                          } }""}"
          )
        ]
      ), 
      JSON = Json.Document(Source)
    in
      JSON

     

    Best Regards,
    Gao

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly --  How to provide sample data in the Power BI Forum