Inspecting Requests
Do you know about inspecting a request you just sent? If not, this lesson is right for you! To give you sense of how to do it, let’s have a look at how to get the URL of the request you sent:
>>> response.request.url
00:00
The requests
library prepares the request before sending it to the server. An example of some of the things that it would prepare would be to validate the headers, to serialize JSON content—like we talked about before. To look at the PreparedRequest
, you use the method .request
—for example, response.request.url
.
00:18
Let’s try it out! To inspect our PreparedRequest
we just need to make one, so let’s save it into a response
again. Since we’re just focusing on this, let’s just go ahead and put it into a post()
and type the URL directly. This time.
00:34
we’ll send the data as JSON and just put a really simple key and value pair here. All right. Let’s look at our response
—success. Great! So, the Response
will have the PreparedRequest
inside of it—again, we access it from .request
—and then if we put another dot (.
) on here we can see all the different other methods that are available. So we can say, “What was the URL for that?
Bartosz Zaczyński RP Team on June 21, 2021
@Shafin Thiyam The HTTP status code 405 means “method not allowed”. You’re most likely viewing the address in a web browser, which sends an HTTP GET request by default. You want to send your data with the POST method instead. To do that, you can use cURL in the command line, for example:
$ curl -i -X POST https://httpbin.org/post -H 'Content-Type: application/json' --data '{"key": 42}'
HTTP/2 200
date: Mon, 21 Jun 2021 06:07:18 GMT
content-type: application/json
content-length: 416
server: gunicorn/19.9.0
access-control-allow-origin: *
access-control-allow-credentials: true
{
"args": {},
"data": "{\"key\": 42}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "11",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0",
"X-Amzn-Trace-Id": "Root=1-60d02c96-7d669c2e4c0324306e437fe4"
},
"json": {
"key": 42
},
"origin": "89.64.35.172",
"url": "https://httpbin.org/post"
}
Become a Member to join the conversation.
Shafin Thiyam on June 20, 2021
urlpost='https://httpbin.org/post'
request raise 405 error