Service Binding call resulting in TypeError

Hello, I'm currently working on implementing Service Bindings but I'm running into the following error when calling the fetch() of the service binding and passing on a string: TypeError: Fetch API cannot load: <MY-STRING> The Env interface:
export interface Env {
MY_SERVICE_BINDING: any;
}
export interface Env {
MY_SERVICE_BINDING: any;
}
The binding in the wrangler.toml:
services = [
{ binding = "MY_SERVICE_BINDING", service = "the-worker-im-calling" }
]
services = [
{ binding = "MY_SERVICE_BINDING", service = "the-worker-im-calling" }
]
The service binding fetch call:
const result = await env.MY_SERVICE_BINDING.fetch("MY-STRING");
const result = await env.MY_SERVICE_BINDING.fetch("MY-STRING");
9 Replies
Chaika
Chaikaโ€ข12mo ago
You need to give it a valid url ex. https://example.invalid/MY-STRING It doesn't matter what it is, just needs to be an absolute url it can load. Can be completely invalid host
2adventure
2adventureOPโ€ข12mo ago
So the format must be an URL even though it's arbitrary data I'm passing on? And then I strip the dummy URL in the other worker receiving the request?
Chaika
Chaikaโ€ข12mo ago
the data type has to be a string, the format has to be a valid absolute url, because of the fetch interface with service bindings you should be able to pass data via the cf object on request/fetch as well, anything you set should just be passed on, can be useful but yea as for how you handle it it's up to. You could just ignore the host and parse the path on the other worker if your goal is to pass information var newURL = new URL(request.url) var path = newURL.pathname;
2adventure
2adventureOPโ€ข12mo ago
How do I access the cf object or is there any documentation about that?
Chaika
Chaikaโ€ข12mo ago
it's just an object you can set in fetch or new requests For example, you can even pass the entire incoming request object and it'll keep the cf properties the same
2adventure
2adventureOPโ€ข12mo ago
Yeah just noticed when the type documentation ๐Ÿ˜„ And the same object is readable in the Request object of destination worker, isn't it? Thanks! Will look into that ๐Ÿ‘
Chaika
Chaikaโ€ข12mo ago
yea, it just gets passed on as is through a service binding
2adventure
2adventureOPโ€ข12mo ago
Alright, thanks for your help ๐Ÿ™‚
Want results from more Discord servers?
Add your server