Next.js Discord

Discord Forum

Why isn't this Axios request making a request to `/api/pages`?

Answered
West Siberian Laika posted this in #help-forum
Open in Discord
West Siberian LaikaOP
import axios from "axios";

export async function postHandle() {
  try {
    const response = await axios({
      method: "post",
      url: "/api/pages",
      headers: {},
      data: {
        handle: "test2",
      },
    });
    console.log("Response:", response);
    return response;
  } catch (error) {
    console.error("Error posting handle:", error);
    throw error;
  }
}

I tried url: "http://localhost:300/api/pages"
and still nothing, not even a log in the console.

this is how I call the request:
"use client";
import { postHandle } from "@/app/lib/actions";

export function Button() {
  return (
    <button
      onClick={() => {
        postHandle;
      }}
      className="flex h-10 items-center rounded-lg bg-blue-600 px-4 text-sm font-medium text-white transition-colors hover:bg-blue-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600"
    >
      <span className="hidden md:block">Create Handle</span>{" "}
    </button>
  );
}
Answered by Toyger
you are not calling your postHandle, it should be
 onClick={() => {
        postHandle();
      }}
View full answer

4 Replies

Toyger
you are not calling your postHandle, it should be
 onClick={() => {
        postHandle();
      }}
Answer
it was that simple
Thank you ^^