juancarlospaco / faster-than-requests

Faster requests on Python 3
https://gist.github.com/juancarlospaco/37da34ed13a609663f55f4466c4dbc3e
MIT License
1.11k stars 90 forks source link

[ new Feature ] Support different HTTP methods #178

Closed MindPatch closed 3 years ago

MindPatch commented 3 years ago

hello, for short requests module python has a function called request with this you can send HTTP requests with any method for example :

import requests ;  requests.request("BLABLA","http://google.com/")

can you add something like that to your project?

best regards

juancarlospaco commented 3 years ago

Kinda like requests.request("GET","http://google.com") ?, whats the benefit over just requests.get("http://google.com/") ?.

MindPatch commented 3 years ago

hi @juancarlospaco we need functions like this in some cases, for example, i made a simple tool for take the method from the user and send http request to the url with the given method , so i'll create a function for take the method allowed and pass it to requests.request function

import requests

def sender(method,url):
      r = requests.request(method,url)

and this allows the user to send HTTP request with any method like CURL with -X option
easy right, but if this function is not available in requests module the code will be like this

def sender(method,url):
       method = method.upper()
       if method == 'GET':
           requests.get(url)
       elif method == 'POST':
          requests.post(url)
       elif method == 'PUT':
          requests.put(url)
       ---  ETC --

also, another function called requests.Request do the same work of requests.request but with empty headers (just Host header)

I hope I explained it clearly

juancarlospaco commented 3 years ago
import requests

def sender(method,url):
      r = requests.request(method,url)

But this would crash if user sends empty string as the method. Or any other value thats not an http method, so it is worse.

def sender(method,url):
       method = method.upper()
       if method == 'GET':
           requests.get(url)
       elif method == 'POST':
          requests.post(url)
       elif method == 'PUT':
          requests.put(url)

That code is better, because it can catch empty string or string that is not an http method.

To make it even better you should not use string for the method, but use an Enum instead, so it is impossible for the value to be empty string nor anything that is not an http method.

You can use empty headers.

juancarlospaco commented 3 years ago

I do not think this improves the API, because it makes it worse and more error-prone. You should use Enum instead of strings.

MindPatch commented 3 years ago

this is an example code of course if this is in a real app I'll add a check for an empty value ok if I use the usual function in requests (get / put /post /etc..), what about XPOST/XPUT methods how can I send requests with those methods? my main question is not about the user but about the developers now I need to make an automation tool by nim for testing HTTP request smuggling, but this bug needs XPOST!

juancarlospaco commented 3 years ago

For GET use get(), for POST use post(), for PUT use put().

This is a lib, not a executable program. If you need to do "manual" HTTP maybe use something like httpie or Insomnia https://insomnia.rest