How to upload Firefox addon file using Node.JS 16 fetch?

I am trying to follow https://blog.mozilla.org/addons/2022/03/17/new-api-for-submitting-and-updating-add-ons/ to upload an addon file. It works fine with cURL but I really want to use Node.js (stuck on 16 at the moment) since I have to build a JWT. I am using the --experimental-fetch flag with node and am trying to avoid adding any more dependencies.

Here’s what I have so far but the endpoint is complaining that the “upload” key is missing from the form data even though I am adding the file stream to the form data using the “upload” key.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const jwt = require('jsonwebtoken');
const fs = require('fs');
var issuedAt = Math.floor(Date.now() / 1000);
var payload = {
iss: process.env.WEB_EXT_API_KEY,
jti: Math.random().toString(),
iat: issuedAt,
exp: issuedAt + 60,
};
var secret = process.env.WEB_EXT_API_SECRET; // store this securely.
var token = jwt.sign(payload, secret, {
algorithm: 'HS256', // HMAC-SHA256 signing algorithm
});
async function main() {
const manifest = require('../manifest.json');
const file_path = `web-ext-artifacts/myaddon-${manifest.version}.zip`
const fileBuffer = fs.createReadStream(file_path); // should this be something else?
const form = new FormData();
form.append('upload', fileBuffer); // Is this wrong?
form.append('channel', 'listed');
console.log('formData', form);
const url = 'https://addons.mozilla.org/api/v5/addons/upload/'
const req = new Request(url, {
method: 'POST',
body: form,
})
req.headers.append('Authorization', `JWT ${token}`);
req.headers.append('Accept', 'application/json');
fetch(req).then(async (res) => {
// console.log('response:', res);
console.log('response.status:', res.status);
const resData = await res.json();
console.log('response.body:', resData);
if (!res.ok) {
throw new Error(`HTTP error! Status: ${res.status}`);
}
})
.catch(err => {
console.error(err);
});
}
main().catch(err => {
console.log(err)
})
</code>
<code>const jwt = require('jsonwebtoken'); const fs = require('fs'); var issuedAt = Math.floor(Date.now() / 1000); var payload = { iss: process.env.WEB_EXT_API_KEY, jti: Math.random().toString(), iat: issuedAt, exp: issuedAt + 60, }; var secret = process.env.WEB_EXT_API_SECRET; // store this securely. var token = jwt.sign(payload, secret, { algorithm: 'HS256', // HMAC-SHA256 signing algorithm }); async function main() { const manifest = require('../manifest.json'); const file_path = `web-ext-artifacts/myaddon-${manifest.version}.zip` const fileBuffer = fs.createReadStream(file_path); // should this be something else? const form = new FormData(); form.append('upload', fileBuffer); // Is this wrong? form.append('channel', 'listed'); console.log('formData', form); const url = 'https://addons.mozilla.org/api/v5/addons/upload/' const req = new Request(url, { method: 'POST', body: form, }) req.headers.append('Authorization', `JWT ${token}`); req.headers.append('Accept', 'application/json'); fetch(req).then(async (res) => { // console.log('response:', res); console.log('response.status:', res.status); const resData = await res.json(); console.log('response.body:', resData); if (!res.ok) { throw new Error(`HTTP error! Status: ${res.status}`); } }) .catch(err => { console.error(err); }); } main().catch(err => { console.log(err) }) </code>
const jwt = require('jsonwebtoken');
const fs = require('fs');

var issuedAt = Math.floor(Date.now() / 1000);
var payload = {
  iss: process.env.WEB_EXT_API_KEY,
  jti: Math.random().toString(),
  iat: issuedAt,
  exp: issuedAt + 60,
};

var secret = process.env.WEB_EXT_API_SECRET;  // store this securely.
var token = jwt.sign(payload, secret, {
  algorithm: 'HS256',  // HMAC-SHA256 signing algorithm
});

async function main() {
    const manifest = require('../manifest.json');
    const file_path = `web-ext-artifacts/myaddon-${manifest.version}.zip`
    const fileBuffer = fs.createReadStream(file_path); // should this be something else?

    const form = new FormData();
    form.append('upload', fileBuffer); // Is this wrong?
    form.append('channel', 'listed');
    console.log('formData', form);


    const url = 'https://addons.mozilla.org/api/v5/addons/upload/'
    const req = new Request(url, {
        method: 'POST',
        body: form,
    })
    req.headers.append('Authorization', `JWT ${token}`);
    req.headers.append('Accept', 'application/json');

    fetch(req).then(async (res) => {
        // console.log('response:', res);
        console.log('response.status:', res.status);

        const resData = await res.json();
        console.log('response.body:', resData);

        if (!res.ok) {
            throw new Error(`HTTP error! Status: ${res.status}`);
        }
    })
    .catch(err => {
        console.error(err);
    });
}

main().catch(err => {
    console.log(err)
})

Output when running the above:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>formData FormData {
[Symbol(state)]: [
{ name: 'upload', value: '[object Object]' },
{ name: 'channel', value: 'listed' }
]
}
(node:2723925) ExperimentalWarning: Fetch is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
response.status: 400
response.body: [ 'Missing "upload" key in multipart file data.' ]
Error: HTTP error! Status: 400
</code>
<code>formData FormData { [Symbol(state)]: [ { name: 'upload', value: '[object Object]' }, { name: 'channel', value: 'listed' } ] } (node:2723925) ExperimentalWarning: Fetch is an experimental feature. This feature could change at any time (Use `node --trace-warnings ...` to show where the warning was created) response.status: 400 response.body: [ 'Missing "upload" key in multipart file data.' ] Error: HTTP error! Status: 400 </code>
formData FormData {
  [Symbol(state)]: [
    { name: 'upload', value: '[object Object]' },
    { name: 'channel', value: 'listed' }
  ]
}
(node:2723925) ExperimentalWarning: Fetch is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
response.status: 400
response.body: [ 'Missing "upload" key in multipart file data.' ]
Error: HTTP error! Status: 400

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật