Questions tagged [python-requests]

USE ONLY FOR THE PYTHON REQUESTS LIBRARY. Requests is a full-featured Python HTTP library with an easy-to-use, logical API.

Official web site

Requests is an HTTP library written in Python under the Apache2 license.

It's meant to simplify HTTP 1.1 related tasks with several functionality out of the box. For example, there’s no need to manually add query strings to your URLs or to form-encode your POST data. Keep-alive and HTTP connection pooling are 100% automatic.

Features:

  • International Domains and URLs
  • Keep-Alive & Connection Pooling
  • Sessions with Cookie Persistence
  • Browser-style SSL Verification
  • Automatic Content Decoding
  • Basic/Digest Authentication
  • Elegant Key/Value Cookies
  • Automatic Decompression
  • Unicode Response Bodies
  • Multipart File Uploads
  • Streaming Downloads
  • Connection Timeouts
  • .netrc support
  • Chunked Requests
  • Python 2.6—3.8
  • Thread-safe.
21751 questions
89
votes
7 answers

Unit testing a python app that uses the requests library

I am writing an application that performs REST operations using Kenneth Reitz's requests library and I'm struggling to find a nice way to unit test these applications, because requests provides its methods via module-level methods. What I want is…
Chris R
  • 17,546
  • 23
  • 105
  • 172
89
votes
17 answers

Python's requests "Missing dependencies for SOCKS support" when using SOCKS5 from Terminal

I'm trying to interact with an API from my Python 2.7 shell using a package that relies on Python's requests. Thing is the remote address is blocked by my network (university library). So to speak to the API I do the following: ~$ ssh -D 8080…
BringBackCommodore64
  • 4,910
  • 3
  • 27
  • 30
89
votes
4 answers

How to send an array using requests.post (Python)? "Value Error: Too many values to unpack"

I'm trying to send an array(list) of requests to the WheniWork API using requests.post, and I keep getting one of two errors. When I send the list as a list, I get an unpacking error, and when I send it as a string, I get an error asking me to…
Bobby Battista
  • 1,985
  • 2
  • 17
  • 27
89
votes
1 answer

Save a large file using the Python requests library

I know that fetching a url is as simple as requests.get and I can get at the raw response body and save it to a file, but for large files, is there a way to stream directly to a file? Like if I'm downloading a movie with it or something?
Matt Williamson
  • 39,165
  • 10
  • 64
  • 72
87
votes
7 answers

Using python Requests with javascript pages

I am trying to use the Requests framework with python (http://docs.python-requests.org/en/latest/) but the page I am trying to get to uses javascript to fetch the info that I want. I have tried to search on the web for a solution but the fact that…
biw
  • 3,000
  • 4
  • 23
  • 40
87
votes
3 answers

Get HTTP Error code from requests.exceptions.HTTPError

I am catching exceptions like this, def get_url_fp(image_url, request_kwargs=None): response = requests.get(some_url, **request_kwargs) response.raise_for_status() return response.raw try: a = "http://example.com" fp =…
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
82
votes
3 answers

Saving response from Requests to file

I'm using Requests to upload a PDF to an API. It is stored as "response" below. I'm trying to write that out to Excel. import requests files = {'f': ('1.pdf', open('1.pdf', 'rb'))} response =…
Chris J. Vargo
  • 2,266
  • 7
  • 28
  • 43
82
votes
7 answers

How to prevent python requests from percent encoding my URLs?

I'm trying to GET an URL of the following format using requests.get() in python: http://api.example.com/export/?format=json&key=site:dummy+type:example+group:wheel #!/usr/local/bin/python import requests print(requests.__versiom__) url =…
Satyen Rai
  • 1,493
  • 1
  • 12
  • 19
81
votes
3 answers

How to receive json data using HTTP POST request in Django 1.6?

I am learning Django 1.6. I want to post some JSON using HTTP POST request and I am using Django for this task for learning. I tried to use request.POST['data'], request.raw_post_data, request.body but none are working for me. my views.py is …
Alok
  • 7,734
  • 8
  • 55
  • 100
80
votes
8 answers

requests: how to disable / bypass proxy

I am getting an url with: r = requests.get("http://myserver.com") As I can see in the 'access.log' of "myserver.com", the client's system proxy is used. But I want to disable using proxies at all with requests.
t777
  • 3,099
  • 8
  • 35
  • 53
77
votes
12 answers

Python: FastAPI error 422 with POST request when sending JSON data

I'm building a simple API to test a database. When I use GET request everything works fine, but if I change to POST, I get 422 Unprocessable Entity error. Here is the FastAPI code: from fastapi import FastAPI app = FastAPI() @app.post("/") def…
Smith
  • 1,037
  • 1
  • 6
  • 15
77
votes
2 answers

how to construct the curl command from python requests module?

Python requests is a good module to ease my web REST API access programming, I usually do like below import json url = 'https://api.github.com/some/endpoint' payload = {'some': 'data'} headers = {'Content-type': 'application/json', 'Accept':…
Larry Cai
  • 55,923
  • 34
  • 110
  • 156
74
votes
8 answers

python requests: how to check for "200 OK"

What is the easiest way to check whether the response received from a requests post was "200 OK" or an error has occurred? I tried doing something like this: .... resp = requests.post(my_endpoint_var, headers=header_var,…
Monty
  • 1,161
  • 2
  • 15
  • 26
74
votes
14 answers

Use python requests to download CSV

Here is my code: import csv import requests with requests.Session() as s: s.post(url, data=payload) download = s.get('url that directly download a csv report') This gives me the access to the csv file. I tried different method to deal with…
viviwill
  • 1,503
  • 3
  • 17
  • 27
73
votes
4 answers

python requests.get() returns improperly decoded text instead of UTF-8?

When the content-type of the server is 'Content-Type:text/html', requests.get() returns improperly encoded data. However, if we have the content type explicitly as 'Content-Type:text/html; charset=utf-8', it returns properly encoded data. Also, when…
arunk2
  • 2,246
  • 3
  • 23
  • 35