Fetch: POST json data

后端 未结 13 1675
小蘑菇
小蘑菇 2020-11-22 03:25

I\'m trying to POST a JSON object using fetch.

From what I can understand, I need to attach a stringified object to the body of the request, e.g.:



        
13条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 04:21

    you can use fill-fetch, which is an extension of fetch. Simply, you can post data as below:

    import { fill } from 'fill-fetch';
    
    const fetcher = fill();
    
    fetcher.config.timeout = 3000;
    fetcher.config.maxConcurrence = 10;
    fetcher.config.baseURL = 'http://www.github.com';
    
    const res = await fetcher.post('/', { a: 1 }, {
        headers: {
            'bearer': '1234'
        }
    });
    

提交回复
热议问题