ALERT!
Click here to register with a few steps and explore all our cool stuff we have to offer!

Jump to content



Photo

beginner guide to python requests


  • Please log in to reply
beginner guide to python requests

#1

someguyagain
someguyagain
    Offline
    34
    Rep
    15
    Likes

    Connor

Posts: 58
Threads: 10
Joined: May 12, 2024
Credits: 0

Half year registered
#1

( P.S THIS IS MY FIRST TIME MAKING NEW TOPIC SO IT WILL BE LACKING SEVERELY I APOLOGISE )

( I need tips for next time anything's appreciated friends. )

( There will be terrible grammar I write this half asleep )

Hey,

 
So I am going to try keep this brief and simple to understand for anyone who is new to python and wants to use the python requests.

 

I am using

 

Python 3.12
 
First to do is your basic

 

pip install requests

( if you haven't already to install the module )

 

Brief summary of website requests

 

Website requests are way of websites returning data and sending data. These requests can be viewed in developer tools / inspect element in the network tab. If you send a request to lets say google you will see numerous results show up on the network tab.

 

Each request has the following

  • Headers
  • Cookies
  • Method ( POST, GET, PUT, DELETE and so on )
  • Path
  • URL

 

Once request is sent, the server returns back data this can be web page so you can view web page / content. Or it can return back information in a JSON format, or cookies which can be used as authentication for your login.

 

Example of JSON Format

{

    "key": "value"

}

 

 

For this requests demonstration we'll use a website called discord

( Since people like discord and it's good way to demonstrate API )

API Documentation: https://discord.com/...pers/docs/intro

 

What you will learn

 

- How to send requests

- Authorization methods

- Sending discord message

- Receiving the response from the request

- Understand what a request is and how it is formed.

 

Sending a request

 

For this example I want you to go on network tab and then open discord and send a message anywhere in a discord server. you will see bunch new request such as returning your profile picture and other information I want you to find a post request under the URL

 

https://discord.com/...IDHere/messages

 

The IDHere is the channel ID unique to that channel so you can send messages there.

( DON'T LOOK IN RESPONSE HEADERS FOR THIS PART )

Look at the request headers

 

You will see some key information

 

Request Method: POST

Status Code: 200

Authorization

Content-Type application/json

 

The content type is way the request should be essentially looked at in simple terms in this case as a json format.

 

Authorization the authorization header is your key, your token for sending requests to backend of discord and they know who you are

 

The key section is the payload section or the json section for some.

 

The json section is displayed as such

 

{

"mobile_network_type":"unknown", NOT NEEDED

"content":"messageContent",

"nonce":"messageId", NOT NEEDED

"tts":false,  NOT NEEDED

"flags":0 NOT NEEDED

}

 

I have marked down which ones aren't needed to send the request so it's easier to understand

Needed ones below:

 

{

"content": "messageContent"

}

 

content is our key, messageContent is our value

python representation

 

 

payload = {

"content": "messageContent"

}

 

print(payload["content"])

 

Now to emulate this request as if we sent a message

Good beginner rules for debugging

  1. Make variable and store the response
  2. Check status codes
  3. check the text before json as if json doesn't exist python will ERROR

Now to send the request here's an example

response = requests.get("https://google.com")

 

We store the response of request object in the response variable. Now lets break it down.

requests is the module so we put requests before the function.

get is the request method what we mentioned earlier

inside of function is the url

 

Getting response information

 

Getting response information is fairly simple we call the response variable we defined and put one of the following

.text

.status_code

.headers

.cookies

.json()

 

There's numerous options that you can see in requests documentation

 

Now how do we add headers to a request?

in the requests.get() or any requests method e.g requests.post. We provide it with headers=YourHeaders

 

Example

requests.get("https://google.com", headers={})

requests.get("https://google.com", headers=headersVariable)

 

Our discord request

 

For our discord request we need to send headers what contain a Authorization token. Lets make a variable what stores the Authorization token in a json format.

headersVariable = {

"Authorization": "YourToken"

}

 

Next lets make a json variable to store our message Content

 

payload = {

"content": "hello world"

}

 

Now how do we finally send this request with the payload?

Code below

import requests

 

payload = {

"content": "hello world"

}

 

headersVariable = {

"Authorization": "YourToken"

}

 

response  = requests.post("https://discord.com/...IDHere/messages", headers=headersVariable, json=payload)

print(response.status_code)

This will hopefully return status of 200

lets break it down

 

We define our json payload as the Content-Type is application/json and pass the required json data in payload section

We define our headers variable to store our authorization what can be easily passed to the request

send the request via requests.post we use post because discord sends information to the API via a POST request method

We pass our headers via headers=

and our json via json=

 

Then we print out sites response code which should be 200 if all is succesful there's numerous status codes a site can return

 

403 - Unauthorized

404- Not Found

329 - Redirect

200 - Succesful

 

and more.

 

 

Now our request does also return some json data what we can get via

response.json()

 

You can also use this to extract key data

 

JSON EXAMPLE

 

{
    "type": 0,
    "channel_id": "channelID",
    "content": "messageContent",
    "attachments": [],
    "embeds": [],
    "timestamp": "UTC Time Sent",
    "edited_timestamp": null,
    "flags": 0,
    "components": [],
    "author": {
        "id": "YourUserID",
        "username": "YourUsername.",
        "avatar": "Avatar",
        "discriminator": "0",
        "public_flags": 0,
        "flags": 0,
        "banner": null,
        "accent_color": null,
        "global_name": "Server Username",
        "avatar_decoration_data": null,
        "banner_color": null,
        "clan": null
    },
    "mentions": [],
    "mention_roles": [],
    "pinned": false,
    "mention_everyone": false,
    "tts": false,
    "nonce": "messageID"
}

 

This is only basic example of sending a message on discord API as I thought this would be great example to demonstrate a post request.

 

GET vs POST Request

 

A get request can only send certain data text type data.

While a post request can send data such as json, XML and other formatting types for the backend to understand

 

Thanks for reading I know it is not the best I am new to nulled.to and wanted to help anyway I could even if no one sees it. It's the thought what counts.


  • 1


 Users browsing this thread: and 1 guests