cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Syndicate_Admin
Administrator
Administrator

how to get cookies from a request to an API?

I am making a request to an endpoint that returns a cookie in the headers, I will use said cookie later in a request that I will pass said cookie as a header. So the steps I need to take are:


1. Make a request to the endpoint that returns the cookie.
2. Make another request to the endpoint where I authenticate with username and password and as header I send the CSRF TOKEN obtained in step 1.
3. I make another request being already authenticated, passing only the Cookie obtained in step 1, said endpoint returns the json that will be the da with which I will work.

What interests me most is being able to obtain the cookie to pass it to the authentication endpoint.
I am currently trying the following to solve the problem in step 1:


 

 

let
     vUrlGetCSRFTOKEN = "https://my-domain.com/api/csrf-cookie",
     TokenBaseUrl = Text.Combine({Uri.Parts(vUrlGetCSRFTOKEN)[Scheme],"://",Uri.Parts(vUrlGetCSRFTOKEN)[Host]}),
     TokenRelativePath = Uri.Parts(vUrlGetCSRFTOKEN)[Path],
     TokenQuery = Uri.Parts(vUrlGetCSRFTOKEN)[Query],

     md = Value.Metadata(Web.Contents(vUrlGetCSRFTOKEN, [
            Headers=[Accept="application/json", Connection="keep-alive",              
                     #"Accept-Encoding"="gzip, deflate"],
            IsRetry=true
          ]
))


in
    md

 

 

But when looking in the headers I don't see the cookie anywhere.

jgarcia_new_4-1653088469455.png

I have previously replicated all this functionality in postman in the following way:

 

 

 

pm.sendRequest({
    url: 'https://my-domain.com/api/csrf-cookie', //This is Step 1, GET THE COOKIES
    method: 'GET',
    headers:{
        'Accept': 'application/json',  
        'Connection':'keep-alive',
        'User-Agent':  'PostmanRuntime/7.29.0',
        'Accept-Encoding': 'gzip, deflate, br',
    },
}, function (error, response, { cookies }) {
    console.log("GET COOKIE");
    console.log(response);
    console.log(cookies);
    if (!error) {
        pm.environment.set('xsrf-cookie', cookies.get('XSRF-TOKEN'));
        pm.sendRequest({
                        url: 'https://my-domain.com/api/auth',
                        method: 'POST',       
                        header:{                            
                            'X-XSRF-TOKEN': cookies.get('XSRF-TOKEN'),//STEP 2, AUTHENTICATE ME AND PASS THE COOKIE FROM STEP 1
                            'Accept': 'application/json',  
                            'Connection':'keep-alive',
                            'User-Agent':  'PostmanRuntime/7.29.0',
                            'Accept-Encoding': 'gzip, deflate, br',                    
                        },
                        body: {
                            mode: 'formdata',
                            formdata: [{        
                                "key": "email",  
                                "value": "email@myemail.com",                               
                            },
                            {        
                                "key": "password",
                                "value":"111122333"
                            }]
                        }
                    }, async function(err, res){
                        console.log("RESULT TO LOGIN");
                        console.log(res);                      
                          pm.sendRequest({
                                        url: 'https://my-domain.com/api/get_list', //GET THE DATA TO WORK
                                        method: 'POST',       
                                        header:{                            
                                            'X-XSRF-TOKEN': cookies.get('XSRF-TOKEN'),
                                            'Accept': 'application/json',  
                                            'Connection':'keep-alive',
                                            'User-Agent':  'PostmanRuntime/7.29.0',
                                            'Accept-Encoding': 'gzip, deflate, br'                                                  
                                        },
                                        body:{
                                            mode:'raw',
                                            raw: JSON.stringify(
                                                {
                                                    page: 1,
                                                    pageSize: 50,
                                                    sort: {},
                                                    type: "page"
                                                }
                                            )
                                        } 
                                    }, async function(err, res){
                                        console.log("RESULT PBI RESPONSE");
                                        console.log(res);
                                    });
                      
                    });
                       
    }
})

 

 

Thank you !

2 REPLIES 2
Syndicate_Admin
Administrator
Administrator

Hi @jgarcia_new ,

 

Maybe this similar post could help you,

Solved: Saving Cookies from POST Request to Rest API - Microsoft Power BI Community

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you for your time, but the link you sent me could help me with step 2 and 3 that I also have to do. But I need to solve step 1, without step 1, I can't get step 2 and step 3. Thank you very much!

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

May UG Leader Call Carousel 768x460.png

June User Group Leader Call

Join us on June 28 for our monthly User Group leader call!

PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

PA.JPG

New Release Planning Portal (Preview)

Check out our new release planning portal, an interactive way to plan and prepare for upcoming features in Power Platform.

Users online (1,459)