如何在 python 中为此 curl 命令发送 post 请求

how to send post request in python for this curl command

如何在 python 中发送与此 curl 命令等效的 POST 请求

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"

我试过下面的代码:

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

它不起作用。

我不知道如何在 POST 请求中发送这一行 "url=https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk"。 "url=https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk" 这是apk的public url。 并想上传到这个 url "https://api-cloud.browserstack.com/app-automate/upload"

应用下面的答案后,我找到了解决方案 谢谢大家。 答案是-

进口urllib.request

#file 将下载到当前工作目录,名称为 app-release.apk urllib.request.url检索('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' 响应 = requests.post(URL, 文件={'file': test_file, }, auth=('YOUR_USERNAME', 'YOUR_ACCESS_KEY'))

使用requests

你可以这样做:

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)

现在应该可以正常工作了。

试试这个

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

或这个

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

解决方案的答案对我来说是

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