requests.post is not giving any response in python? - python

I'm using Python requests 2.19.1 .
But I'm facing an intermittent issue where I get no response at all when I post to a specific url.
I'm trying to check if the LDAP is giving me the expected output for invalid credentials.
Here's the format:
requests.post('https://oxhp-member.uhc.com/Member/MemberPortal/j_acegi_security_check',
credentials_payload)
that I'm posting
Almost everytime, it works fine. But sometimes, it doesn't give any response for that. Even network issues gives us some response. Right? Why am I not getting any response for the above call.
Is there any existing bug in requests?
Somebody please point me in correct direction.

requests is not responsible for "giving back response". The server you are using requests to post to is.
To see the response you have to keep it in a variable and handle it somehow.
resp = requests.post('https://oxhp-member.uhc.com/Member/MemberPortal/j_acegi_security_check',
credentials_payload)
print(resp.status_code)
print(resp.content)
Whatever resp contains is the responsibility of the server.

Related

Expected X-Requested-With header' python requests

So I've been trying to use requests so I can log in to replit.com to access files from my account and pull them to me, however on the log in part it says 'Expected X-Requested-With header', and for the status code it keeps giving me 403 (I think that means forbidden). Also to mention that requests.get() works completely fine, however the method I use has to be for a field where the method is POST. I tried to see the HTML response, however I think there is only the Expected X-Requested With header. Can anyone tell me what is wrong with my code? (down below)
response = requests.post("https://replit.com/login", auth = (email, password), allow_redirects = True)
Also even if the requests.post is only the URL it still doesn't work. Any help?
Edit:
I figured out how to do the headers, it turned out the website had its own header so I used that, but now I can't bypass recaptcha.

Why don't I get a response from my request?

I'm trying to make one simple request:
ua=UserAgent()
req = requests.get('https://www.casasbahia.com.br/' , headers={'User-Agent':ua.random})
I would understand if I received <Response [403] or something like that, but instead, a recive nothing, the code keep runing with no response.
using logging I see:
I know I could use a timeout to avoid keeping the code running, but I just want to understand why I don't get an response
thanks in advance
I never used this API before, but from what I researched on here just now, there are sites that can block requests from fake users.
So, for reproducing this example on my PC, I installed fake_useragent and requests modules on my Python 3.10, and tried to execute your script. It turns out that with my Authentic UserAgent string, the request can be done. When printed on the console, req.text shows the entire HTML file received from the request.
But if I try again with a fake user agent, using ua.random, it fails. The site was probably developed to detect and reject requests from fake agents (or bots).
Though again, this is just theory. I have no ways to access this site's server files to comprove it.

Python request send post request with params not as dictionary

So this seems like it would be a simple thing to do but I can't seem to get it to work. I want to send a post request to a local flask server running on my computer. So I use the following line of code to do it
r = requests.post(end_url + '/upload/flash', cookies=cookie, params='title=asdfg&description=asdfg&front1=f&back1=b&number=1&edu_level=&school=&course=&grade=&unit=&chapter=&section=&tag=')
The problem is not with the params I'm passing into the function as they are formatted correctly. The problem is that the flask server is trying to access the "title" arg from the request but can't find it. All of the examples I have seen format the params in a dictionary (ex.
{"title": "asdfg"}
I would imagine that it is possible to send a request the way I am trying but am not sure what I have done wrong.
The problem was actually not with the params. I was mixing up "params" and "data". I also needed to include the headers
headers={"Content-Type": "application/x-www-form-urlencoded"}
Thanks to everyone who help me troubleshoot my code.

YELP API code expected to return error 401, but instead error 400 is returned

I am learning about YELP APIs currently, and want to execute Business Search, but i am stuck with error 400 (HTTP 400 - Bad request). Following online course, my code (runned in virtual environment) should return error 401 (HTTP 401) (and then we provide created yelp app id and key etc.), but i am getting error 400 and can't find a way to solve this, so i get the error 401 and can continue...
Here is my code, it is super-simple and i checked for grammer mistakes and can't find one. I am looking for answer for few hours and really starting lose myself.... Could someone please help me?
import requests
response = requests.get("https://api.yelp.com/v3/businesses/search") # to send GET (to read) request to an endpoint.
print(response)
Outcome when running code:
<Response [400]>
Please let me know what more information you need to help me out. I am a complete beginner here and begging for help.
According to the Yelp documentation, you are required to include query parameters of either (1) location, or (2) latitude and longitude. It looks like your request is missing those parameters.
https://www.yelp.com/developers/documentation/v3/business_search
See below for example:
import requests
response = requests.get("https://api.yelp.com/v3/businesses/search?latitude=37.786882&longitude=-122.399972") # to send GET (to read) request to an endpoint.
print(response)

What is the difference between HTTP Post URL with /post and without using Python requests module?

I am using Python 2.7 with the requests module to send http post with parameters. I encountered a strange problem.
To do http post, it is just one line;
x = requests.post(URL, params)
I have no problem with the params. It is the URL that puzzled me.
Sometimes, this URL http://hostname/path/post works. Sometimes, I use http://hostname/path without the /post to get the HTTP post to work. I am puzzled why is this so. What is the difference between the two? Under what conditions do I use which one?
'http://hostname/path/post' is a path. You could in principle issue and HTTP GET request to that same path (although probably you wouldn't get anything meaningful back).
In general, you should look at the site's API documentation and post to the url that they say you should post to without adding anything extra to the url.
There are two different concepts, url and HTTP method. You are confused by trying to mix them.
url - an address you talk to
The url is addressing something on some server. If you get valid url, you can take it as a string, do not read in, and use it. Consider it to be a string.
If I would link it to a visiting your friend, url is address of a doors to come to.
HTTP method (POST, GET, DELETE...)
There are multiple HTTP methods which differ in the way, how you talk to given url.
Linking it to visiting a friend, it would be the way, you try to make the doors open (use the bell, knock or use a hammer)

Categories

Resources