hyper
hyper copied to clipboard
how to pass a file and body at same time?
i've new in hyper i want just send request using file for a body but i didn't get valid argument for this
let say i have code like this:
from hyper import HTTPConnection
file = open(myfile, "rb")
body = {
"content":b"mycontent"
}
req = HTTPConnection("myhost.com")
req.request("GET","/path", body=body)
response = req.get_response()
print(response)
i dont know how to pass my file into request argument, so many appreciate for any help
Shouldn't you be using POST instead of GET to send a body? Also, note that body= is expecting a byte string, and you are passing it a dict.
Shouldn't you be using POST instead of GET to send a body? Also, note that
body=is expecting a byte string, and you are passing it a dict.
ahh sorry about that im forgot this one, but you can explain me how to pass valid argument for POST method pass a file and some data(body)?
I am also having some issue with POST. here is my sample code.
filepath = "testfile" print(type(filepath)) file_size = os.stat(filepath).st_size print("file_size = {}".format(file_size)) a = open(filepath, 'rb')
files = {'image', a}
hdr = {'Content-Length': str(file_size)} conn.request(method='POST', url="/upload_http2/upload_http2.php", body=files, headers=hdr) resp = conn.get_response() print(resp.status)
This part of the docs provides an example POST request:
from hyper import HTTPConnection
c = HTTPConnection('http2bin.org')
req = c.request('POST', '/post', body=b'hello')
As you can see, body= is given a bytestring b'hello'. You could convert your dicts to string and encode them to a bytestring? that might work.
This part of the docs provides an example POST request:
from hyper import HTTPConnection c = HTTPConnection('http2bin.org') req = c.request('POST', '/post', body=b'hello')As you can see,
body=is given a bytestringb'hello'. You could convert your dicts to string and encode them to a bytestring? that might work.
i know about this one, but how if i want post image
for example i use default requests
import requests
fr = {
"a":open("tes.jpg", "rb")
}
r = requests.post("https://api.ians.web.id/api/img/index.php", files=fr)
print(r.text)
and i get what i want, im so glad if have example using hyper for my issue
Currently there is no way to do this in hyper. There are two ways I can see this being resolved:
- Implement the functionality manually within Hyper
- Re-use requests implementation, such at this.
For the time being, in theory I reckon you could just use the requests method I listed to encode the data yourself and pass that to hyper in the body? I'm not sure what the thoughts of the repo owner are on re-using requests stuff to implement functionality but I don't see a reason not to