Ask A Question

Notifications

You’re not receiving notifications from this thread.

Help parsing a string down to what I need it to be.

RJ McCollam asked in Ruby

I am working on an API integration and I have to authenticate via one call, take cookie values from the response, and use them in a new call. Using HTTParty I can make all the calls I need to, but my issue comes with parsing the set-cookie response I get from my first call into what it needs to be for my second call.

Here is what I get from my first call for set-cookie:

ASP.NET_SessionId=tyq0iz2apxli30wih0xdf0jt; path=/; HttpOnly; SameSite=Lax, UserBranch=1; path=/, Locale=Culture=en-US&TimeZone=GMTM0700S; expires=Thu, 22-Apr-2021 14:35:47 GMT; path=/, CompanyID=Sandbox; expires=Thu, 22-Apr-2021 14:35:47 GMT; path=/, .ASPXAUTH=1869AD7D19D10655EBBD673C06E9EE641156F95DAA9ADFCD938F3836F916AAEA90CD8FCE23783C55E82F6E66C1A9DAD4B743B33AC033F52954D04526B050C7C77AEF6209A05238B2C79B61391F0E5DC14C7EC4CEFDBDD15DE4234E2DF2ED20B09011C817A9D61E3CFDB1E2787B6C9BDA825173FF2B7BCB7D35AC67615412BD13547DA958CFF05F62AADB94BE480B0E6A1A966F36; path=/; HttpOnly, ASP.NET_SessionId=tyq0iz2apxli30wih0xdf0jt; path=/; HttpOnly; SameSite=Lax, UserBranch=1; path=/, Locale=Culture=en-US&TimeZone=GMTM0700S; expires=Thu, 22-Apr-2021 14:35:47 GMT; path=/, CompanyID=Sandbox; expires=Thu, 22-Apr-2021 14:35:47 GMT; path=/, .ASPXAUTH=1869AD7D19D10655EBBD673C06E9EE641156F95DAA9ADFCD938F3836F916AAEA90CD8FCE23783C55E82F6E66C1A9DAD4B743B33AC033F52954D04526B050C7C77AEF6209A05238B2C79B61391F0E5DC14C7EC4CEFDBDD15DE4234E2DF2ED20B09011C817A9D61E3CFDB1E2787B6C9BDA825173FF2B7BCB7D35AC67615412BD13547DA958CFF05F62AADB94BE480B0E6A1A966F36; path=/; HttpOnly, requestid=693977198F02788111EBA11C82CB901C; path=/, requeststat=+st:92+sc:~/entity/auth/login+start:637544397471740106+tg:; path=/

Here is what I need it to be in order to successfully make my second call:

.ASPXAUTH=1869AD7D19D10655EBBD673C06E9EE641156F95DAA9ADFCD938F3836F916AAEA90CD8FCE23783C55E82F6E66C1A9DAD4B743B33AC033F52954D04526B050C7C77AEF6209A05238B2C79B61391F0E5DC14C7EC4CEFDBDD15DE4234E2DF2ED20B09011C817A9D61E3CFDB1E2787B6C9BDA825173FF2B7BCB7D35AC67615412BD13547DA958CFF05F62AADB94BE480B0E6A1A966F36; ASP.NET_SessionId=tyq0iz2apxli30wih0xdf0jt; CompanyID=Sandbox; Locale=Culture=en-US&TimeZone=GMTM0700S; UserBranch=1; requestid=693977198F02788111EBA11C82CB901C; requeststat=+st:92+sc:~/entity/auth/login+start:637544397471740106+tg:;

I have two questions:

  1. What is an efficient way to remove the items I do not need. Just multiple gsubs?
  2. What can I do about duplicate values in the response I get from the first call? For instance I get multiple .ASPXAUTH entities that are duplicates of each other. How do I only strip one of them out?

Of course everything works nice and neat in Postman, but trying to implement it I am running into these issues.

Reply

Does this work? https://stackoverflow.com/questions/54979195/passing-cookies-with-httparty

get_response = HTTParty.get(url)

cookie_hash = HTTParty::CookieHash.new
get_response.get_fields('Set-Cookie').each { |c| cookie_hash.add_cookies(c) }

response = HTTParty.post("http://localhost:3000/users/other_urls", {headers: {'Cookie' => cookie_hash.to_cookie_string }} )
Reply

It does, thank you Chris. Need to spend more time working through the code to see available options like this since they aren't documented.

Appreciate the help.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.