10 examples of 'fetch api post' in JavaScript

Every line of 'fetch api post' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
110public async fetchPost<T>(requestURL: string, body: TBody, query?: string, requestOption?: RequestInit): Promise<T> {
111 const finalRequestOption: RequestInit = Object.assign({
112 method: 'POST',
113 headers: this.headers
114 }, { body }, requestOption);
115 const response: Response = await this.httpClient.fetch(this.getRequestURL(requestURL, query), finalRequestOption);
116 return response.json();
117}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

61static async post(url, data) {
62 return await Fetch.request(
63 url,
64 'POST',
65 data,
66 );
67}
14post(url, data) {
15 return new Promise((resolve, reject) => {
16 fetch(url, {
17 method: 'POST',
18 headers: {
19 'Content-type': 'application/json'
20 },
21 body: JSON.stringify(data)
22 })
23 .then(res => res.json())
24 .then(data => resolve(data))
25 .catch(err => reject(err));
26 });
27}
36post(url, opts = {}) {
37 const defaults = {
38 method: 'POST',
39 headers: this._getHeaders()
40 };
41
42 return fetch(URL.resolve(this.baseUrl, url), merge(defaults, opts))
43 .then(this._handleResponse).catch(Fetch._handleError);
44}
8function fetchApi (endpoint, opts = {}) {
9 opts = assign({
10 headers: {
11 'Content-Type': 'application/json'
12 }
13 }, base.opts, opts)
14 return fetch(`${base.url}/api/${endpoint}`, opts)
15 .then(response => {
16 if (response.status !== 200) { throw response }
17 switch (response.headers.get('Content-Type')) {
18 case 'application/json': return response.json()
19 default: return response.text()
20 }
21 })
22}
256function directPost(url, params, headers = {}) {
257 return directRequest('POST', url, params, headers);
258}
42export function post(url, paramsObj) {
43 var result = fetch(url, {
44 method: 'POST',
45 credentials: 'include',
46 headers: {
47 'Accept': 'application/json, text/plain, */*',
48 'Content-Type': 'application/x-www-form-urlencoded'// 默认表单提交
49 },
50 body: obj2params(paramsObj)
51 });
52
53 return result;
54}
34post(endpoint: string, body: any, options?: RequestOptions) {
35 return this.http.post(this.url + '/' + endpoint, body, options);
36}
51createPost(data) {
52 const url = this._getFullUrl("/posts");
53 const request = () => this.httpClient.post(url, {...data, is_published: true});
54 return this._handleRequest(request);
55}
17static fetchAllPosts() {
18 return facade.get("/posts");
19}

Related snippets