How to format HTTP request to discord API?

2024/11/16 13:45:29

While this code works, it sends "Bad Request".

import socket, ssl
token = 'NzMyMzQ1MTcwNjK2MTR5OEU3.XrzQug.BQzbrckR-THB9eRwZi3Dn08BWrM'
HOST = "discord.com"
PORT = 443
t = 'POST / HTTP/1.0\r\nAuthentication: Bot {token}\r\nHost: discord.com/api/guilds/{702627382091186318}/channels\r\n\r\n'
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s_sock = context.wrap_socket(s, server_hostname=HOST)
s_sock.connect((HOST, 443))
s_sock.sendall(t.encode())f = s_sock.recv(7000).decode()
print(f)s_sock.close()

Note: this is not a real token.

Answer
t = 'POST / HTTP/1.0\r\nAuthentication: Bot {token}\r\nHost: discord.com/api/guilds/{702627382091186318}/channels\r\n\r\n'

This is not a valid HTTP request. You essentially send (line breaks added for clarity):

 POST / HTTP/1.0\r\nAuthentication: Bot {token}\r\nHost: discord.com/api/guilds/{702627382091186318}/channels\r\n\r\n

But a correct POST request would look like this instead:

 POST /api/guilds/{702627382091186318}/channels HTTP/1.0\r\nAuthentication: Bot {token}\r\nHost: discord.com\r\nContent-length: ...\r\n<body, where size matches Content-length header>

I.e. you have the wrong path, wrong Host header, missing body and missing Content-length header. If you really want to write your own HTTP stack instead of using existing libraries please study the standards instead of just guessing how it might look - that what standards are for.

https://en.xdnf.cn/q/120325.html

Related Q&A

Python 3.30 TypeError: object of type int has no len() [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

Is it possible to disable negative indexing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

Google Colab Notebook completely freezes when training a YOLO model

I am following a tutorial to train custom object detection model using YOLO. This is the tutorial and also where I got the Notebook Everything works fine until the training bit which is the last cell. …

Generate 4 columns of data such that each row sum to 100

How do I write a python program that can randomly generate 4 columns of data such that the sum of the numbers of each row is 100?

Nan to Num Python

I have multiple array that for those I calculate a linear regression, but sometimes it gives me 0/0 values which gives me a NaN. I know that to convert an array where there are numbers that are NaN you…

Class constructor able to init with an instance of the same class object

Can python create a class that can be initialised with an instance of the same class object?Ive tried this:class Class(): def __init__(self,**kwargs):print selfself = kwargs.get(obj,self)print selfif …

Python Logical Operators

I am currently learning Python3, but I dont understand Logical Operators. Here is the link: http://pymbook.readthedocs.org/en/py3/operatorsexpressions.html#logical-operators First of all, it seems that…

Partial Pivoting In Pandas SQL Or Spark

Partial Pivoting In Pandas SQL Or Spark Make the Year remain as Rows, and have the States Transpose to columns Take Pecentage value of a Gender Male Race White, InputOutput

Python to create a find-replace dictionary from a word vba macro

I have a very big macro Selection.Find.ClearFormattingSelection.Find.Replacement.ClearFormattingWith Selection.Find.Text = "asa".Replacement.Text = "fsa".Forward = True.Wrap = wdFin…

Converting many .txt files into csv and combining them

I have many .txt files. I want to convert a few files ending with specific names into csv and combine them into one csv. ### Folder Name: text_files python_gramm.py aadd01.txt aaxx02.txt aaff03.txt hhd…