Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mostafa8026/3da63736048d55866f16d757696c7b32 to your computer and use it in GitHub Desktop.
Save mostafa8026/3da63736048d55866f16d757696c7b32 to your computer and use it in GitHub Desktop.
got test post
import got, { HTTPError, Method, Options } from "got/dist/source";
import * as fs from "fs";
import FormData from "form-data";
const gotExtended = got.extend({
hooks: {
beforeRetry: [
(options, error, retryCount) => {
console.log("beforeRetry", retryCount);
},
],
beforeRequest: [
(options) => {
console.log("beforeRequest");
},
],
},
});
start();
async function start() {
var response = await request();
console.log(response);
}
function post(url, options: Options = {}, usePoolProxy = true) {
return send("POST", url, options, usePoolProxy);
}
function send(method: Method, url, options: Options = {}, usePoolProxy = true) {
const newOption = {
...options,
method,
};
return proxyRequest(url, newOption, usePoolProxy, gotExtended, method);
}
function proxyRequest(
url: string | URL,
options: Options,
usePoolProxy: boolean,
callback,
method?: Method | "stream"
) {
return callback(url, options);
}
async function request() {
try {
const form = new FormData();
const fileStream = fs.createReadStream("/home/mostafa/test.png");
form.append("myfile", fileStream);
/** upTo is one of these 2x, 4x, 8x so upTo.slice(0, 1) has to be 2, 4, 8 */
form.append("scaleRadio", "4");
form.append("Alg", "slow");
const options: Options = {
headers: {
"Content-Type": `multipart/form-data; boundary=${form.getBoundary()}`,
},
body: form,
https: { rejectUnauthorized: false },
throwHttpErrors: false,
retry: { limit: 2, methods: ["GET", "POST"], statusCodes: [405, 403] },
};
console.log("get created");
const response: any = await post(
"https://access2.imglarger.com:8999/upload",
options
);
return response.body;
} catch (error: any) {
console.log(error.response);
return error;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment