How to upload file to DHC-REST/HTTP API with Selenium

720 views Asked by At

I'm trying to send POST request with Chrome extension DHC https://chrome.google.com/webstore/detail/dhc-resthttp-api-client/aejoelaoggembcahagimdiliamlcdmfm?hl=en (I know that there are a lot of tools to send http requests, but I need to use this one). I wrote following code:

from selenium.webdriver.chrome.options import Options
from selenium import webdriver

# Add Option to be able to use DHC extension
chrome_options = Options()
chrome_options.add_extension('/usr/lib/firefox/browser/extensions/aejoelaoggembcahagimdiliamlcdmfm-0.8.6.2-www.Crx4Chrome.com.crx')
# Start browser session
dr = webdriver.Chrome(chrome_options=chrome_options)
# Open Extension page
dr.get('chrome-extension://aejoelaoggembcahagimdiliamlcdmfm/dhc.html')
# Close update pop-up if exists
try:
    dr.find_element_by_xpath('//a[@class="btn btn-info"]').click()
except:
    pass
# Change HTTP request type to POST
request_type = dr.find_elements_by_xpath('//input[@class="gwt-TextBox"]')[1]    
request_type.clear()
request_type.send_keys("POST")
# Choose body type 'file'
dr.find_element_by_xpath('//div[@class="pane-head"]').click()
dr.find_elements_by_xpath('//li[@class="dropdown right"]')[1].click()
dr.find_element_by_xpath('//a[contains(text(), "file")]').click()
# Set path to file I want to send
dr.find_element_by_xpath('//input[@class="gwt-FileUpload"]').send_keys('/path/to/file/dummy3gb')

After last step name of uploaded file appears, but actual file size is 0 bytes and I can't send request as content is not recognizedenter image description here while if to choose file manually from upload prompt proper file size is displayed and I can send request enter image description here

Am I doing something wrong? Any ideas?

1

There are 1 answers

0
Alok Bichhwe On

When you are want to test your API from DHC for the post method. there has an option for input type like first one is test and second one is file. According to the image.

enter image description here

When you select the file from drop down you can post your file for your web service.