Python requests params proper encoding - python

I am having an issue with a http request I make using the python requests lib. When I make this query using the browsers, this is how the request looks like:
mysite/test?query=name%3A"My+Name+Is"*
Now, when I try to use the python lib, I use this dictionary as my params:
{'query' : 'name:My Name Is*'}
When I do this query using the browser, it works however, when I do this using the python lib, it does not work. Now my question is, should I be escaping the '*'? or are the spaces messing this up? the python lib should already be encoding this string so I would think I don’t need to do that prior to passing it. Any advice or comments are appreciated!
Here is a sample of the code making the request:
url = 'https://rink.hockeyapp.net/api/2/apps/app_id/app_versions/version_id/crash_reasons/search'
params={'per_page': 100, 'page': 1, 'query': 'reason:"Some String With Spaces*"'}
requests.get(url, params=params)

Related

How to send dictionary in params Python Requests Library

Can someone help me please I am having a difficult time trying to figure out how to send a dictionary in the params of a Get Request using Python's Request Library. Below is what the Param looks like in inspect element. I have tried several different combinations to get this work, and i keep getting an error from the API saying {'error_details': "required selection'sellout_inclusion' is missing.",'data': None}. Any help on this woudl be much appreciated. Thanks.
Param
selections: {sellout_inclusion: ["Include All"]}
To send a get request to the server you can define a dictionary with params:
Example:
import requests
PARAMS = {"selections": {'sellout_inclusion': ["Include All"]}}
res = requests.get('https://www.google.com/', params=PARAMS)
print(res.text)
the problem can be with the API you request data from
you can read this article maybe it will help if my example didn't
https://jakevdp.github.io/PythonDataScienceHandbook/03.04-missing-values.html

GET requests works only with params not just url

I've just discovered something strange. When downloading data from facebook with GET using the requests 2.18.4 library, I get error when I just use
requests.get('https://.../{}/likes?acces_token={}'.format(userID,token))
into which I parse the user ID and access - the API does not read the access token correctly.
But, it works fine as
requests.get('https://../{}'.format(userID), params={"access_token":token})
Or it works when I copy paste the values in the appropriate fields by hand in the python console.
So my hypothesis is that it has something to with how the token string got parsed using the params vs the string. But what I don't understand at all, why would that be the case? Or is ? character somehow strange in this case?
Double check if both the URLs are the same (in your post they differ by the /likes substring).
Then you can check how the library requests concatenated parameters from the params argument:
url = 'https://facebook.com/.../{}'.format(userID)
r = requests.Request('GET', url, params={"access_token":token})
pr = r.prepare()
print pr.url

Python Urllib2 Post Data Request Formatting

Normally a request would be formatted in Python as:
varName = {'username': 'password'}
The normal request would be:
username=password
My problem is the request that I am trying to send is:
{"part1":"part2"}
I don't know how to format this for python. I simply need assistance with formatting it. Thank you.

Is a request response always JSON?

I'm trying to understand what exactly I'm getting back when I make a POST request using the Requests module — is it always JSON? Seems like every response I get appears to be JSON, but I'm not sure.
Where r is my response object, when I do:
print r.apparent_encoding
It always seems to return ascii
And when I try type():
>>>print type(r)
<class 'requests.models.Response'
I pasted the output from print r.text into a JSON validator, and it reported no errors. So should I assume Requests is providing my with JSON objects here?
A response can be anything. If you've posted to a REST endpoint, it will usually respond with JSON. If so, requests will detect that and allow you to decode it via the .json() method.
But it's perfectly possible for you to post to a normal web URL, in effect pretending to be a browser, and unless the server is doing something really clever it will just respond with the standard HTML it would serve to the browser. In that case, doing response.json() will raise a ValueError.
No, the response text for a POST request is totally up to the web service. A good REST API will always respond with JSON, but you will not always get that.
Example
A common pattern in PHP is
<?php
$successful_whatever = false;
if (isset($_POST['whatever'])) {
# put $_POST['whatever'] in a database
$successful_whatever = true;
}
echo $twig->render('gallery.twig',
array('successful_whatever' => $successful_whatever));
?>
As you can see the response text will be a rendered template (HTML). I'm not saying it is good, just that it is common.

HTTP POST and parsing JSON with Scrapy

I have a site that I want to extract data from. The data retrieval is very straight forward.
It takes the parameters using HTTP POST and returns a JSON object. So, I have a list of queries that I want to do and then repeat at certain intervals to update a database. Is scrapy suitable for this or should I be using something else?
I don't actually need to follow links but I do need to send multiple requests at the same time.
How does looks like the POST request? There are many variations, like simple query parameters (?a=1&b=2), form-like payload (the body contains a=1&b=2), or any other kind of payload (the body contains a string in some format, like json or xml).
In scrapy is fairly straightforward to make POST requests, see: http://doc.scrapy.org/en/latest/topics/request-response.html#request-usage-examples
For example, you may need something like this:
# Warning: take care of the undefined variables and modules!
def start_requests(self):
payload = {"a": 1, "b": 2}
yield Request(url, self.parse_data, method="POST", body=urllib.urlencode(payload))
def parse_data(self, response):
# do stuff with data...
data = json.loads(response.body)
For handling requests and retrieving response, scrapy is more than enough. And to parse JSON, just use the json module in the standard library:
import json
data = ...
json_data = json.loads(data)
Hope this helps!
Based on my understanding of the question, you just want to fetch/scrape data from a web page at certain intervals. Scrapy is generally used for crawling.
If you just want to make http post requests you might consider using the python requests library.

Categories