Translation of a query string to python dictionary for requests.post - python

I'm trying to make a login in a web page with requests. It's a post request so I'm using a dictionary to introduce the parameters. However, the query string to translate is quite weird and I'm not getting any success passing it to a dict. The query to translate looks as follow:
userid=...&password=...&submit=*Iniciar+sesi%C3%B3n&title=
The part of the query which is causing to me some troubles is
submit=*Iniciar+sesi%C3%B3n
I don't know how to turn it correctly to the dictionary to be well interpreted by the request.

This isn't a full answer but I can't comment due to reputation.
I'm not sure how *Iniciar+sesi%C3%B3n ought to be parsed, but you can use cgi.parse_qs or urllib.parse.parse_qs to parse the query string into a dict. This will result in a 'submit': ['*Iniciar sesi\xc3\xb3n'] entry. In order to get around this, you could write code to explicitly parse the value of the submit into something that your code can use later on.

Related

Oracle cloud REST API WaasPolicy and auditEvents pagination

I am trying to figure out the exact query string to successfully get the next page of results for both the waasPolicy logs and auditEvents logs. I have successfully made a query to both endpoints and returned data but the documentation does not provide any examples of how to do pagination.
my example endpoint url string:
https://audit.us-phoenix-1.oraclecloud.com/20190901/auditEvents?compartmentId={}&startTime=2021-02-01T00:22:00Z&endTime=2021-02-10T00:22:00Z
I have of course omitted my compartmentId. When I perform a GET request against this url, it successfully returns data. In order to paginate, the documentation states:
"Make a new GET request against the same URL, modified by setting the page query parameter to the value from the opc-next-page header. Repeat this process until you get a response without an opc-next-page header. The absence of this header indicates that you have reached the last page of the list."
My question is what exactly is this meant to look like? An example would be very helpful. The response header 'opc-next-page' for the auditEvents pagination contains a very long string of characters. Am I meant to append this to the url in the GET request? Would it simply be something like this? of course replacing $('opc-next-page') with that long string in the header.
https://audit.us-phoenix-1.oraclecloud.com/20190901/auditEvents?compartmentId={}&startTime=2021-02-01T00:22:00Z&endTime=2021-02-10T00:22:00Z&page=$(opc-next-page)
And the query for waasPolicy:
https://waas.us-phoenix-1.oraclecloud.com/20181116/waasPolicies/{}/wafLogs
returns an opc-next-page header in the form of a page number. Would it simply require appending something like &page=2? (Tried this to no avail)
Again, I am not able to find any examples in the documentation.
https://docs.oracle.com/en-us/iaas/api/#/en/waas/20181116/WaasPolicy/GetWaasPolicy
https://docs.oracle.com/en-us/iaas/Content/API/Concepts/usingapi.htm#nine
Thank you in advance for your help
Found the answer. Needed to specify &page=$(opc-next-page) AND specify a &limit=X (where x = any integer i.e. 500) parameter. Without the limit param, the &page= param returns a 500 error which is slightly misleading. Will leave this up for anyone else stumbling upon this issue.

Python - parse graphql, add something, then back to the query string

I'm trying to use python to parse a graphql query, alter it, and turn it back into a string query that I can pass to the graphql server.
Specifically, I'm trying to ensure that a query will always have the pageInfo paging information, so if I'm executing a query, I will always be able to automatically page through the results, even if a user might forget that stanza in their actual query.
It seems surprisingly difficult to parse a graphql query into something useful, and then be able to go from the parsed data representation back to the query string. Is there a library that google isn't able to turn up for me?
Thanks so much for any help!
The core library includes a parse function for parsing strings into AST and a print_ast function for converting the AST back to a string.

Trying to grab a section of a URL and then amend it to the end of a line in python

So I am trying to a session ID from the end of a URL, and then add to another URL. I am basically opening an internal site which goes to a homepage, and then to search for an item, goes to another page. To get around having to give out the password we use a script for it, which we currently use autohotkey, which doesn't work very well, has a lot of issues, and generally is more of a pain than just loading the site and logging in.
So here is my progress:
First I tried:
sid = urlparse(browser.current_url).query
url=
urljoin('http://internal.site/BelManage/find_pc_by_name.asp?',
sid)
That failed, which makes sense. So then I imported urlencode and did:
updateurl='http://internal.site/BelManage/find_pc_by_name.asp?{}'.format(urllib.parse.urlencode(sid))
This fails stating not a valid non-string sequence or mapping object.
Because I have to grab the sid with .query from urlparse it means I cannot use a string concatenation, unless I convert sid as a set to a string, which I am not for sure of an easy way to do that.
Any ideas of a better way to do this?
Ok I forgot everything, and then remembered, and was able to get this working.
I converted sid to a str via str(sid), defined the url variable with the address and a simple concatenation and it seems to be working now.

JSON Response Weirdly Formatted

I would like to preface my question that this is the first time I've interacted with an API and JSON as I'm typically more on the Database sides of things.
With that, I'm a little confused with one of the APIs I'm currently working with.
I have a vendor that has an API that allows me to pull down some information about some of the users of that service. The problem is that the response seems to not be in JSON, or if it is it isn't a version of JSON that I have seen.
The response looks like this.
{"Header":"Field1,Field2,Field3,Field4", "Rows":["Row1Value1,Row1Value2,Row1Value3,Row1Value4","Row2Value1,Row2Value2,Row2Value3,Row2Value4"]}
Which, seems wrong with everything that I've been doing with JSON so far. I'm unable to interpret this in Python as anything use-able or Powershell.
Is this a type of format? Or is this some weird thing that this vendor has generated that isn't JSON and needs to be interpreted as it's own thing?
It looks like a half-JSON implementation; the outer containers look like JSON, and you get a JSON list for the rows, but the inner contents of Header and each row in Rows looks like a string you'll need to tokenize yourself (split on commas).
I think there is a bit of confusion here. JSON means literally just JavaScript Object Notation. Anything that parses to a valid object in JS and is limited to the data types String, Bool, Int, Float, Array and Object is JSON.
So, is this JSON? Yes, beyond doubt. Is this good JSON? Not really. Unfortunately, the idea would be that you would be able to parse a JSON object into a tabular form, but here, you would have to split things yourself.
Using simple string manipulation (split()), you can easily parse the rows and restructure them to your heart's content.

Getting a URL with Python

I'm trying to do something similar to placekitten.com, wherein a user can input two strings after the base URL and have those strings alter the output. I'm doing this in Python, and I cannot for the life of me figure out how to grab the URL. In PHP I can do it with query string and $_REQUEST. I can't find a similar method in Python that doesn't rely on CGI.
(I know I could do this with Django, but that's serious overkill for this project.)
This is just by looking at the docs but have you tried it?
cherrypy.request.path_info
The docs say:
The ‘relative path’ portion of the Request-URI. This is relative to the script_name (‘mount point’) of the application which is handling this request.
http://docs.cherrypy.org/stable/refman/_cprequest.html#cherrypy._cprequest.Request.path_info

Categories

Resources