diff --git a/src/routes/about/+page.svelte b/src/routes/about/+page.svelte index 75b60a5..88fcd3e 100644 --- a/src/routes/about/+page.svelte +++ b/src/routes/about/+page.svelte @@ -168,11 +168,28 @@

Examples

- curl -X POST -H "Content-Type: application/json" -d '{'{'}"contents": "paste contents"{'}'}' https://0x45.st/api/pastes +
+		
JSON Request
+ + $ curl -X POST -H "Content-Type: application/json" -d '{'{'}"contents": "paste contents"{'}'}' https://0x45.st/api/pastes + +
- curl -X POST -F "contents=paste contents" https://0x45.st/api/pastes - - curl -X POST -d "paste contents" https://0x45.st/api/pastes +
+		
Form Request
+ + $ curl -X POST -F "contents=paste contents" https://0x45.st/api/pastes
+ # or, with a file
+ $ curl -X POST -F "contents=@file-name.txt" https://0x45.st/api/pastes +
+
+ +
+		
Plaintext Request
+ + $ curl -X POST -H "Content-Type: text/plain" -d "paste contents" https://0x45.st/api/pastes + +

If the paste was successfully created, the API will respond with the following JSON:

diff --git a/src/routes/api/pastes/+server.ts b/src/routes/api/pastes/+server.ts index 71c15e7..c080c55 100644 --- a/src/routes/api/pastes/+server.ts +++ b/src/routes/api/pastes/+server.ts @@ -28,18 +28,26 @@ const isTrue = (value: string | boolean | undefined): boolean => { // Extract the options from the form data. const extractOptionsFromForm = async (req: Request): Promise => { const form = await req.formData(); - const contents = form.get('contents') as string; + const contents = form.get('contents'); const language = form.get('language') as string; const password = form.get('password') as string; const burnAfterReading = form.get('burnAfterReading') as string; const raw = form.get('raw') as string; - if (!contents || contents.length === 0) { + let text: string; + // Check if contents is a file, if so read it. + if (contents instanceof File) { + text = await contents.text(); + } else { + text = contents as string; + } + + if (!text || text.length === 0) { throw error(400, 'No contents provided for your paste.') } return { - contents, + contents: text, language, password, burnAfterReading: isTrue(burnAfterReading), @@ -50,7 +58,6 @@ const extractOptionsFromForm = async (req: Request): Promise => { // Extract the options from the JSON body. const extractOptionsFromJSON = async (req: Request): Promise => { const { contents, language, password, burnAfterReading, raw } = await req.json(); - console.log(contents, language, password, burnAfterReading, raw); if (!contents || contents.length === 0) { throw error(400, 'No contents provided for your paste.') @@ -70,9 +77,7 @@ const extractOptionsFromQuery = async (req: Request): Promise => { const url = new URL(req.url); const query = url.searchParams; - const reader = req.body?.getReader(); - const body = await reader?.read(); - const contents = body?.value?.toString(); + const contents = await req.text(); if (!contents) { throw error(400, 'No contents provided for your paste.') @@ -152,6 +157,6 @@ export const POST: RequestHandler = async ({ request }) => { status: 201, }); } else { - return text(data.url); + return text(data.url + '\n'); } }; \ No newline at end of file