digitalmars.D.learn - Trying to extract string from curl redirected URL
- Josh (16/16) Feb 10 2019 I have a website (say https://website.com) that I need to log in
- Anonymouse (16/19) Feb 10 2019 Assuming I'm understanding your question (you want to get the
I have a website (say https://website.com) that I need to log in to, it will do a few 302 redirects and then I will end up with a unique session ID in the URL (such as https://website.com/welcome.html?s=636853677441448706). Is there some way of extracting this ID (I'll need it later for other things)? I'm using std.net.curl's HTTP, and when I turn verbose on and do the login POST I can see that it is in fact redirecting to the URL with the session ID. I can see that etc.c.curl has CurlInfo.effective_url and CurlInfo.redirect_url that I can use with curl_easy_getinfo, but that needs a CURL*. HTTP.handle.handle is private, so I can't get a CURL* out of the Curl struct. Is there something I'm missing? It it possible to access the private handle? Is there a better way to do what I'm trying to achieve?
Feb 10 2019
On Sunday, 10 February 2019 at 14:12:02 UTC, Josh wrote:Is there something I'm missing? It it possible to access the private handle? Is there a better way to do what I'm trying to achieve?Assuming I'm understanding your question (you want to get the path of a URL after redirects), if you're not adverse to dependencies you could use requests; https://code.dlang.org/packages/requests. Create a Request, query an URL for a Response and then access its finalURI member. Then parse the path string normally to get the session ID, such as with std.algorithm.findSplit and friends or with regex. import requests; Request req; Response res = req.get("https://website.com/"); string id = res.finalURI.path .findSplit("?s=")[2] .findSplitBefore("&")[0]; if (!id.length) { /* no ID in URL */ }
Feb 10 2019