juancarlospaco / faster-than-requests

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

use faster-than-requests for post form-data? #129

Closed sxcisco closed 3 years ago

sxcisco commented 3 years ago

with requests

name="1.jpg"
with open("1.jpg", "rb") as f:
                content=f.read()
files = {
    'Filedata': (name, content, "image/jpeg"),
    "from": (None, "user"),
    "isRetImgAttr": (None, "1"),
}
requests.post(url, files=files,verify=False,timeout=5)

Is it possible to use faster-than-requests for something like that?

juancarlospaco commented 3 years ago
sxcisco commented 3 years ago

`POST /test.html HTTP/1.1 Host: example.org Content-Type: multipart/form-data;boundary="boundary"

--boundary Content-Disposition: form-data; name="field1"

value1 --boundary Content-Disposition: form-data; name="field2"; filename="example.txt"

value2`

Is it possible to use faster-than-requests for something like that?`

juancarlospaco commented 3 years ago

I do not understand what specifically you do not understand, to send data on the body use the body argument, theres example for body on the repo, to send data on the multipart data use the multipart_data argument, theres example for multipart_data on the repo, to send content type use the http_headers argument, all "data" is just a string, maybe practice on http://httpbin.org/post

sxcisco commented 3 years ago

I know how to use the multipart_data argument, My problem is how to send the "data-binary" parameter with faster-than-requests Content-Disposition: form-data; name="picture"; filename="123.jpg" Content-Type: image/jpeg Can you help me give my example? thank you.

juancarlospaco commented 3 years ago
import faster_than_requests

print( 
  faster_than_requests.post(
    url            = "http://httpbin.org/post",
    body           = "body data here",
    http_headers   = [("Content-Type", "text/plain"), ("dnt", "1")],
    multipart_data = [("multi", "part"), ("key", "value")],
  ) 
)

Plain text data is just string, binary data is just string too, HTTP over the wire is all just strings anyway.

If you want to send a file, just read it into a string variable, like mydata = open("file.txt").read().

If you like you can use bytes() instead of string too.

For timeout, we have miliseconds precision, so it can be timeout = 5000.

All arguments of all functions are simple primitive types, like string, bool, integer, etc.

We do not have any non-primitive binary blob thing, because no need for it and HTTP itself does not has it.

I dont know how to make it more explicit than that.