English 中文(简体)
NextJS 13 Data Fetching
原标题:

Is there anyway of custom baseURL for their fetch method?

For example in axios:

const instance = axios.create({
  baseURL:  https://some-domain.com/api/ ,
  timeout: 1000,
  headers: { X-Custom-Header :  foobar }
});
问题回答

As per my understanding you want to consume a native method to fetch data(API).
For that JS has a fetch( url) method. If you just want to fetch data from an end point, then just pass the url and you will get the result (Use await with it)

let data=fetch( https://some-domain.com/api/ );

Its easy way, or you can define the whole body with header.

fetch("https://some-domain.com/api/", {
  method: "POST",
  body: JSON.stringify({
    your: data,
  }),
  headers: {
    "Content-type": "application/json; charset=UTF-8"
  }
});

Above snippet is for POST method. Other methods are similar to this.

I think there is no such thing as baseUrl for fetch Api.

You can instead create a wrapper from a fetchApi, so that you only need to pass the URL once.

const fetchWrapper = (path: string) => {
   return fetch(`${BASE_URL}${path}`)
}




相关问题
NextJS 13 Data Fetching

Is there anyway of custom baseURL for their fetch method? For example in axios: const instance = axios.create({ baseURL: https://some-domain.com/api/ , timeout: 1000, headers: { X-Custom-Header ...

/bin/sh: get: command not found

When I try to run a script by cron I get this error messaege: /bin/sh: get: command not found I also tried it in bash shell, and I tried curl , wget and fetch but non of them helped. Can ...

Hibernate Criteria: Eagerly load ManyToMany collection

My problem seems pretty straightforward to me, but I just can t seem to find an answer that works. I have a Hibernate Entity which has a ManyToMany association with another Entity, which is fetched ...

iPhone CoreData Queries

I m new at using CoreData and I m trying to understand how to perform a query on a table. I can use a fetch request to pull all of the records from a table, but I m looking for a subset. Is there an ...

Git svn fetch retrieving only one revision at a time

I m using Git-1.6.5.1-preview20091022.exe. I ve cloned a SubVersion repository using: git svn clone -s https://xxxxx:8443/svn/project/SubProjectA The SubProjectA has the standard layout (trunk, ...

热门标签