You are writing a Python client that sends a POST request with a JSON body to a REST endpoint using the requests library. The server requires basic authentication credentials and expects the response in JSON format. You want to ensure the request uses HTTP Basic Auth and automatically parses the JSON response. Which code snippet correctly accomplishes this?
You are building a Python application that interacts with a REST API. The API requires you to send a GET request with query parameters for filtering results. You decide to use the 'requests' library. Which of the following code snippets correctly sends a GET request to 'https://api.example.com/items' with the query parameters 'category=books' and 'limit=10'?
You are writing a Python client that must call a REST API which sometimes returns HTTP 429 (Too Many Requests) with a 'Retry-After' header. Your current code uses requests.get() and raises an exception on any 4xx status. You need to automatically wait for the server-specified interval and retry the request up to three times. Which approach correctly implements this behavior?
You are writing a Python client that consumes a REST API. The API documentation states that the server returns a JSON object with a top-level key 'items', but for a specific endpoint the response body is an empty JSON object '{}'. You need to distinguish between a missing 'items' key and a present but empty list. Which approach correctly handles this without raising an exception?
You are developing a Python script that retrieves data from a REST API using the 'requests' library. The API returns JSON responses, but occasionally the server responds with a 500 Internal Server Error and an HTML error page. Your script must handle these errors gracefully by logging the status code and not crashing. Which of the following approaches is most appropriate for robust error handling?
You are developing a Python application that must send a large file to a REST API using the 'requests' library. The server expects the file to be streamed to avoid high memory usage on the client. Which method should you use to achieve this?
You are building a Python client that consumes a REST API which returns JSON. You want to handle common error responses gracefully. Which TWO of the following are correct practices when using the requests library to process an API response? (Choose two.)
You are designing a REST API client in Python that must handle HTTP redirects and authentication. The API uses OAuth 2.0 with bearer tokens and may redirect to a different host for authentication. You need to configure the 'requests' library to follow redirects, preserve the Authorization header across redirects, and handle token refresh. Which two of the following approaches are correct? (Choose two.)
You are using the `requests` library to interact with a REST API. The API returns JSON data, and you need to extract a specific value from a nested structure. After calling `response = requests.get(url)`, which line of code correctly parses the JSON response and accesses the value of the key 'name' inside the 'user' object?
You are building a Python REST API client that needs to send a JSON payload in a POST request using the `requests` library. The API expects the JSON data to be properly serialized and the `Content-Type` header set to `application/json`. Which code snippet correctly accomplishes this?
You are using the socket module to create a TCP client. After creating the socket object, which method call is required to establish a connection to a remote server listening on 192.168.1.50 at port 8080?
You are consuming a REST API that returns data in JSON format. After calling response.json(), you notice the data contains nested dictionaries. What is the most Pythonic way to handle a missing key in the response dictionary safely?
You are building a REST client that needs to handle 429 Too Many Requests status codes. Which strategy is most effective when implementing an automatic retry mechanism?
When using the 'requests' library to perform a GET request, which parameter should be passed to the 'params' argument to include query string parameters in the URL?
You are developing a multithreaded HTTP scraper. When using the 'requests' library inside 'threading.Thread' objects, what is the recommended practice for handling session persistence?
You are investigating a network bottleneck in your Python application using `requests`. If you aren't using a `Session` object, what performance-related behavior should you expect?
You wrote a small REST client that calls `requests.get('https://api.example.com/v1/items', timeout=5)`. Under normal network load the call succeeds, but the server occasionally takes longer than 5 seconds to start sending the response body while the TCP connection is already established. You want to allow up to 5 seconds for the connection to be established and up to 30 seconds for the server to send data after the connection is made. Which call achieves this?
You are designing a REST client that must call an endpoint protected by OAuth 2.0 using the client credentials grant. The authorization server expects the client identifier and client secret to be sent in the request body as `application/x-www-form-urlencoded`, along with `grant_type=client_credentials`. Which `requests` call correctly obtains the token?
You are writing a Python tool that must connect to a REST API over HTTPS. The API's server certificate is signed by an internal corporate CA whose root certificate is stored in a file named corp-ca.pem. You use the requests library and need the connection to be verified against that specific CA while still performing normal hostname checking. Which call accomplishes this?
You are writing a REST client that must handle HTTP error responses robustly. For each response, you want to detect server errors and extract the error message returned in a JSON body. Which TWO of the following approaches are correct and safe? (Choose two.)
A Python script consumes a REST endpoint that returns a JSON document. You call response = requests.get(url) and want to convert the body into native Python objects. The response is guaranteed to be valid JSON. Which expression gives you the deserialized data?
You are developing a Python application that needs to retrieve data from a public REST API. The API returns JSON data and requires you to send a GET request with a custom User-Agent header. Which of the following code snippets correctly sends the request using the requests library?
You are writing a script that downloads a large file from a REST endpoint using `requests.get(url, stream=True)`. You want to write the content to disk in chunks rather than loading it all into memory. Which approach correctly implements chunked reading?
You are building a client for a REST API that requires an OAuth 2.0 bearer token. The token is long-lived and stored in an environment variable. You want every request in a session to carry the Authorization header without repeating the header setup at each call site. Which approach is correct and idiomatic with the requests library?
You are designing a Python REST API client that must handle HTTP errors gracefully. The client uses the requests library. Which TWO of the following statements about error handling with requests are correct? (Choose two.)
Your team uses a REST API that requires a Bearer token in the Authorization header for every request. You want to configure a `requests.Session` so that all requests made through it automatically include this header, and you also want to reuse the underlying TCP connection across calls. Which approach is correct?
You are writing a Python client to consume a REST API. The API documentation states that the server may respond with a 307 Temporary Redirect when a resource has moved temporarily. You need to ensure that your client, built with the 'requests' library, preserves the original HTTP method and body when following such redirects. Which of the following best describes how 'requests' handles a 307 redirect by default?
You are consuming a paginated REST API that returns a JSON object with a 'data' list and a 'next' field holding the URL of the following page, or null on the last page. You want a loop that retrieves every record exactly once. Which TWO statements about implementing this loop correctly are true? (Choose two.)
You are building a Python script that consumes a REST API which returns paginated JSON responses. The API includes a 'next' link in the response body for the next page. You need to follow these links until all pages are retrieved. Which approach is most appropriate using the requests library?
You are writing a client that must upload a binary file to a REST endpoint using requests.put(). The file is several hundred megabytes, and you want to avoid loading it entirely into memory while still letting the server know the total size. Which approach meets both goals?
You are designing a Python microservice that exposes a REST API. A client sends a PUT request to update a resource with a JSON body. Your service must ensure idempotency: if the same request is received multiple times, the resource should end up in the same state. Which HTTP method and semantics should you implement to guarantee idempotency for updates?
You are writing a Python script that needs to send a GET request to a REST API and parse the JSON response. The API requires an API key passed as a query parameter. Which code snippet correctly performs this request using the requests library?
You are implementing a REST API client in Python using the 'requests' library. The API requires authentication using OAuth 2.0. You need to obtain an access token and then use it to authenticate subsequent requests. Which TWO of the following statements about handling OAuth 2.0 with 'requests' are correct? (Choose two.)
You are writing a Python script that must download a large file from a REST API endpoint. The file is several gigabytes, and you want to avoid loading it entirely into memory. Which approach using the requests library is most memory-efficient?
You are designing a Python application that consumes a REST API which returns JSON data. The API responses can be large, and you need to process the JSON incrementally to avoid loading the entire response into memory. The server supports chunked transfer encoding. Which of the following techniques should you use with the 'requests' library to stream the response and parse it incrementally?
You are writing a Python script that needs to parse a URL and extract the query parameters. The URL is 'https://api.example.com/v1/items?limit=10&offset=20'. Which module should you use to reliably parse this URL and retrieve the value of 'limit'?
You are implementing a Python client that communicates with a REST API over HTTPS. The API uses a self-signed certificate. You need to verify the server's certificate against a custom CA bundle while also ensuring that the hostname is verified. Which approach correctly configures the 'requests' library?
You are writing a Python script that uses the `socket` module to create a TCP client. After calling `sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)`, which method should you call next to initiate a connection to a remote server at `('example.com', 80)`?
You are developing a Python application that uses the `requests` library to consume a REST API. You need to implement a retry mechanism for transient HTTP errors (e.g., 500, 502, 503, 504) and connection timeouts. Which TWO of the following approaches are valid and recommended for achieving this with `requests`? (Choose two.)
You are implementing a Python client that uses the `socket` module to communicate with a server. The server sends data in chunks, and you need to ensure you receive all data until the connection is closed by the server. Which approach correctly reads all data from the socket?