1

i'm trying to upload an apk to server the code i used to upload using curl is written like this:

curl -u "musername:mypass" -X POST "https://myurl/upload" -F "file=@D:\path\to\location.apk"

i tried to make a script using python with requests library like this:

response = requests.post(
            self.urlUpload,
            files={"file" : open(self.apkLocation, 'r')},
            auth=(self.BSUsername, self.BSToken)
        )

but it gives me errors:

{"error":"Malformed archive"}

anyone knows why this erros appeared?

2
  • 3
    Try reading it in rb ? Commented Sep 12, 2019 at 14:06
  • You also need to set the content type headers={'Content-Type': 'application/octet-stream'} Commented Sep 12, 2019 at 14:33

1 Answer 1

2

Have you had a chance to try something like below?

import requests

    files = {
        'file': ('/path/to/app/file/Application-debug.apk', open('/path/to/app/file/Application-debug.apk', 'rb')),
    }
    response = requests.post('https://api-cloud.browserstack.com/app-automate/upload', 
                files=files, 
                auth=('BSUsername ', 'BSToken'))
    print (response.text)#THE APP URL (bs://<hashed appid>) RETURNED IN THE RESPONSE OF THIE CALL

Check the BrowserStack REST API doc here

Sign up to request clarification or add additional context in comments.

2 Comments

Welcome and nice answer Maddy! Thumbs up for including the link, however you could improve your answer by explaining your code or including comments. This way people may have a better chance of understanding if they're not experts :)
ah exactly! i replaced the "r" flag with "rb" flag just like yours and that works. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.