/* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the "Elastic License * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side * Public License v 1"; you may not use this file except in compliance with, at * your election, the "Elastic License 2.0", the "GNU Affero General Public * License v3.0 only", or the "Server Side Public License, v 1". */ import React, { useCallback } from 'react'; import { useState } from 'react'; import { EuiText, EuiButton, EuiLoadingSpinner, EuiFieldText, EuiCallOut, EuiFormRow, EuiTextArea, } from '@elastic/eui'; import { type IHttpFetchError, isHttpFetchError } from '@kbn/core-http-browser'; import { Services } from './services'; interface Props { postMessage: Services['postMessage']; addSuccessToast: Services['addSuccessToast']; } export function PostMessageRouteExample({ postMessage, addSuccessToast }: Props) { const [error, setError] = useState(); const [isPosting, setIsPosting] = useState(false); const [message, setMessage] = useState(''); const [id, setId] = useState(''); const doFetch = useCallback(async () => { if (isPosting) return; setIsPosting(true); const response = await postMessage(message, id); if (response && isHttpFetchError(response)) { setError(response); } else { setError(undefined); addSuccessToast('Message was added!'); setMessage(''); setId(''); } setIsPosting(false); }, [isPosting, postMessage, addSuccessToast, setMessage, message, id]); return (

POST example with body

This examples uses a simple POST route that takes a body parameter and an id as a param in the route path.

setId(e.target.value)} data-test-subj="routingExampleSetMessageId" /> setMessage(e.target.value)} /> doFetch()} > {isPosting ? : 'Post message'} {error !== undefined ? ( {error.message} ) : null}
); }