Uploading runs via API, getting 'Method Not Allowed' (status 405) #812
-
Hello, I am trying to upload runs using Node.js and the node-fetch package, but it won't work. Here is the code: // Import modules
import fetch from 'node-fetch';
// Import files
import { splits } from './Splits.js'
export const upload = async () => {
const probe = await fetch(`https://splits.io/api/v4/runs`, {
method: "POST"
});
const response = await probe.json();
//console.log(response);
if (response.status !== 201) {
return 'Upload failed.';
}
const body = new URLSearchParams();
for (const key in response.presigned_request.fields) {
body.append(key, response.presigned_request.fields[key]);
}
body.append('file', JSON.stringify(splits));
const post = await fetch(response.presigned_request.uri, {
method: response.presigned_request.method,
body: body
});
//console.log(post);
return `Upload complete. Claim your splits: ${response.uris.claim_uri}`;
} Using
I'm unsure how to proceed. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The splits-io/app/javascript/upload.js Lines 21 to 51 in f6b4086 |
Beta Was this translation helpful? Give feedback.
-
It would appear that using the Working code, in case it helps others in the future: // Import modules
import * as fs from 'fs';
import fetch from 'node-fetch';
import { FormData } from "formdata-node"
// Import files
import { config } from '../index.js';
import { splits } from './Splits.js';
export const upload = async () => {
const probe = await fetch(`https://splits.io/api/v4/runs`, {
method: "POST"
});
const response = await probe.json();
if (response.status !== 201) {
return 'Upload failed.';
}
const body = new FormData();
for (const key in response.presigned_request.fields) {
body.set(key, response.presigned_request.fields[key]);
}
body.set('file', fs.readFileSync(`${config.splitsPath}/${splits.fileName}.json`));
const post = await fetch(response.presigned_request.uri, {
method: response.presigned_request.method,
body: body
});
return `Upload complete. Claim your splits: ${response.uris.claim_uri}`;
} |
Beta Was this translation helpful? Give feedback.
It would appear that using the
formdata-node
package along withfs.readFileSync()
instead of the JS object worked. Thanks!Working code, in case it helps others in the future: