Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

how to pass a file and body at same time? #410

Open
Alnyz opened this issue May 1, 2019 · 6 comments
Open

how to pass a file and body at same time? #410

Alnyz opened this issue May 1, 2019 · 6 comments

Comments

@Alnyz
Copy link

Alnyz commented May 1, 2019

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

@dhdavvie
Copy link

dhdavvie commented May 1, 2019

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.

@Alnyz
Copy link
Author

Alnyz commented May 2, 2019

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)?

@abhijeetgituser
Copy link

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)

@dhdavvie
Copy link

dhdavvie commented May 2, 2019

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.

@Alnyz
Copy link
Author

Alnyz commented May 2, 2019

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.

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

@dhdavvie
Copy link

dhdavvie commented May 2, 2019

Currently there is no way to do this in hyper. There are two ways I can see this being resolved:

  1. Implement the functionality manually within Hyper
  2. 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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants