【问题标题】:Error while trying to upload apk file to Browserstack cloud, using Axios / node-fetch尝试使用 Axios / node-fetch 将 apk 文件上传到 Browserstack 云时出错
【发布时间】:2019-02-19 11:55:06
【问题描述】:

我正在尝试以编程方式将 .apk / .ipa 文件上传到 browserstack 云(而不是运行 curl 命令)

选项 1:节点获取 api

const myfetch = require('node-fetch');

const buildToPost = {
   file: '</my path>'
};

const options = {
   method: 'POST',
   body: JSON.stringify(buildToPost)
};

myfetch('https://</myusername>:</mykey>@api.browserstack.com/app-automate/upload', options)
   .then(res => res.json())
   .then(res => console.log(res))
   .catch(error => console.error('Error:', error));​

但它给出以下错误:

{ 错误:'格式无效。有关有效 API,请参阅 REST API 文档 格式 - https://www.browserstack.com/app-automate/rest-api' }

选项 2:Axios API

    const axios = require('axios');

axios.post('https://</myusername>:</mykey>​@api-cloud.browserstack.com/app-automate/upload', {
      File: '</my path>​'
   })
   .then
   ((response) => {
      console.log(response);
   }).catch((error) => {
      console.log((error));
})​

错误:数据:

{ 错误: '无效的格式。有关有效的 API 格式,请参阅 REST API 文档 - https://www.browserstack.com/app-automate/rest-api' } } }

卷曲命令参考:

curl -u "</myusername>:</mykey>" -X POST https://api-cloud.browserstack.com/app-automate/upload -F "file=@/path/to/app/file/Application-debug.apk" -F 'data={"custom_id": "MyApp"}'

Browserstack sample link

【问题讨论】:

  • 注意:要上传的文件存在于本地磁盘/文件夹中

标签: axios fetch webdriver-io browserstack


【解决方案1】:

下面是使用axios 的方法。重点是:

  • 身份验证(使用user 选项)
  • 使用FormData 模块提交多部分数据
  • maxContentLength 选项设置得足够高以允许上传您的文件。

代码如下。

import axios from 'axios';
import fs from 'fs';
import FormData from 'form-data';

const formData = new FormData();

// Open file stream
const newFile = fs.createReadStream(binaryPath);

// Add form field params
formData.append('file', newFile, 'my_filename.apk');
formData.append('custom_id', 'npm_uploaded_apk');

axios({
  url: 'https://api-cloud.browserstack.com/app-automate/upload',
  method: 'post',
  headers: formData.getHeaders(),
  auth: {
    username:'my_browserstack_username',
    password: 'my_browserstack_access_key',
  },
  data: formData,
  maxContentLength: 1073741824,
})
  .then(response => {
    // The object with the 'app_url' parameter is in the 'data' field of the response.
    console.log('POST successful: ', response.data);
  })
  .catch((error) => {
    console.log('POST error: ', error);
  });

this GitHub thread 中的更多背景信息。

【讨论】:

    猜你喜欢
    • 2014-01-27
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 2020-01-10
    • 2020-11-11
    • 2012-10-26
    • 2017-08-27
    • 2022-06-23
    相关资源
    最近更新 更多