'Pass a prop value as a query string to a sveltekit endpoint
I'm trying to pass a prop value from a text input field to a Sveltekit endpoint, but I can't figure out how I pass it through.
My onsubmit runs this function
const handleSubmit = () => {
promise = getData();
}
Which calls a function to get data from an external API via a sveltekit endpoint:
async function getData() {
const response = await self.fetch('api/address.json');
const addressdata = await response.json()
if (response.ok) {
return addressdata;
}
}
The end point looks like this
export async function get({ }) {
const baseurl = 'https://myaddressapi/?postcode=xxxxxx'
const res = await fetch(baseurl, {
method: 'GET',
headers: {'Accept': 'application/json',
'APIKey': 'mykey'}
})
const data = await res.json()
if (res.ok) {
return {
status: 200,
body: data
}
}
return {
status: res.status,
error: new Error('This is an error')
}
}
I want to be able to do something like:
const response = await self.fetch('api/address.json', {props:{searchInput}});
and then capture it on the endpoint:
export async function get(request, props)
const baseurl = 'https://myaddressapi/?postcode='
const url = `${baseurl}${props}`
I've tried to look at the documentation, but I can't seem to get it to work. Any pointers, please?
Solution 1:[1]
I re-read the docs, and I needed to pass the postcode as an ID into the endpoint, which is now working.
More information here - https://kit.svelte.dev/docs/routing#endpoints
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ron |
