denoland / fresh

The next-gen web framework.
https://fresh.deno.dev
MIT License
12.46k stars 643 forks source link

get request body from local api handlers #493

Closed bobwatcherx closed 2 years ago

bobwatcherx commented 2 years ago

How to get the request body from the POST method in the handler? but i get error

ReadableStream { locked: false }

my code : import { Handlers } from "$fresh/server.ts";

export const handler: Handlers = {

async POST(req){ const body = await req.body; console.log(body); return new Response("tollol"); } };

and secondly how to fetch local api from /api/joke to islands component

file :/islands/myhome.tsx export const handler: Handlers= { async GET() { const query = await fetch("http://localhost:8000/api/joke") <<<==== THIS console.log(query) return new Response(query) },

};

iamsahilsonawane commented 2 years ago
  1. How to get the request body from the POST method in the handler?
import { Handlers } from "$fresh/server.ts";

export const handler: Handlers = {

async POST(req) {
    const body = await req.json();
    console.log(body);
    return new Response(JSON.stringify(body), {
      headers: { "Content-Type": "application/json" },
    });
  }
}
  1. How to fetch local API from /api/joke to islands component

Why would you add hander to the island component? Instead, add the hander in the route where the island component is being used/rendered.

For e.g. You'll add a handler for the index.ts instead of the counter on the island.

bobwatcherx commented 2 years ago
  1. How to get the request body from the POST method in the handler?
import { Handlers } from "$fresh/server.ts";

export const handler: Handlers = {

async POST(req) {
    const body = await req.json();
    console.log(body);
    return new Response(JSON.stringify(body), {
      headers: { "Content-Type": "application/json" },
    });
  }
}
  1. How to fetch local API from /api/joke to islands component

Why would you add hander to the island component? Instead, add the hander in the route where the island component is being used/rendered.

For e.g. You'll add a handler for the index.ts instead of the counter on the island.

if i use on page not on island does fetch('/api/joke') work?

Thanks

iamsahilsonawane commented 2 years ago
  1. How to get the request body from the POST method in the handler?
import { Handlers } from "$fresh/server.ts";

export const handler: Handlers = {

async POST(req) {
    const body = await req.json();
    console.log(body);
    return new Response(JSON.stringify(body), {
      headers: { "Content-Type": "application/json" },
    });
  }
}
  1. How to fetch local API from /api/joke to islands component

Why would you add hander to the island component? Instead, add the hander in the route where the island component is being used/rendered. For e.g. You'll add a handler for the index.ts instead of the counter on the island.

if i use on page not on island does fetch('/api/joke') work?

Thanks

Yes, await fetch("http://localhost:8000/api/joke") works on any page handler!