简体   繁体   中英

how to send post request in python for this curl command

how to send a POST request in python equivalent with this curl command

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/upload" \
-F "url=https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk"

I tried code below:

resp=requests.post(URL,headers= 
    {'YOUR_USERNAME:YOUR_ACCESS_KEY'},
  data=https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk") 

and its not working.

I don't know how to send this line "url=https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk" in POST request. "url=https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk" this is the public url of apk. and want to upload at this url "https://api-cloud.browserstack.com/app-automate/upload"

And after applying answer from below, I find the solution Thank you Everyone. Answer is -

import urllib.request

#file will download in current working directory with name app-release.apk urllib.request.urlretrieve('https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk', 'app-release.apk')

test_file = open("app-release.apk", "rb")

URL = 'https://api-cloud.browserstack.com/app-automate/upload' response = requests.post(URL, files={'file': test_file, }, auth=('YOUR_USERNAME', 'YOUR_ACCESS_KEY'))

Use requests

You can do like this:

import requests

files = { 'file': ('url=https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk', 
open('url=https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk', 
'rb')),}
URL = 'https://api-cloud.browserstack.com/app-automate/upload'
response = requests.post(URL, files=files, auth=('YOUR_USERNAME', 'YOUR_ACCESS_KEY'))
print (response.text)

It should work fine now.

try this

import requests

files = {
    'url': (None, 'https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk'),
}

response = requests.post('https://api-cloud.browserstack.com/app-automate/upload', files=files, auth=('YOUR_USERNAME', 'YOUR_ACCESS_KEY'))

or this

import requests

files = {
    'url': 'https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk'
}

response = requests.post('https://api-cloud.browserstack.com/app-automate/upload', files=files, auth=('YOUR_USERNAME', 'YOUR_ACCESS_KEY'))
'''

And The answer of the solution, That is work for me is

import urllib.request

#file will download in current working directory with name app-release.apk

urllib.request.urlretrieve('https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk', 'app-release.apk')

test_file = open("app-release.apk", "rb")

URL = 'https://api-cloud.browserstack.com/app-automate/upload' response = requests.post(URL, files={'file': test_file, }, auth=('YOUR_USERNAME', 'YOUR_ACCESS_KEY'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM