Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can fetch function work with a base url? #4439

Closed
saibing opened this issue Mar 20, 2020 · 8 comments
Closed

How can fetch function work with a base url? #4439

saibing opened this issue Mar 20, 2020 · 8 comments
Labels

Comments

@saibing
Copy link

saibing commented Mar 20, 2020

const result = await fetch("/u-route/baas/doc");
console.log(result);

this code worked in browser but does not in deno. deno will output as follow:

ompile file:///data/saibing/git/ts/deno/fetch.ts
error: Uncaught URIError: relative URL without a base
► $deno$/errors.ts:35:13
    at constructError ($deno$/errors.ts:35:13)
    at unwrapResponse ($deno$/dispatch_json.ts:41:12)
    at sendAsync ($deno$/dispatch_json.ts:96:10)
@ry
Copy link
Member

ry commented Mar 20, 2020

What would you expect to happen?

@kitsonk
Copy link
Contributor

kitsonk commented Mar 21, 2020

Ref #203 as well.

@nayeemrmn
Copy link
Collaborator

nayeemrmn commented Mar 21, 2020

The only way thing that would makes sense is for it to work relative to cwd (or the fs root for absolute paths), the resource resolution root. so ref #2150 and see #4038 (comment) onwards.

@saibing
Copy link
Author

saibing commented Mar 21, 2020

@ry

I recently tried to use deno as my testing tool for a web server project. I want to reuse the front-end SDK library, which uses a lot of this kind URL:

   async verify (params: Object) {
     let url = "/baas/auth/v1.0/resetpassword/step?step=verify";
     const resp = await fetch (url, {
       method: "POST",
       headers: {
         "content-type": "application / json"
       },
       credentials: "same-origin",
       body: JSON.stringify (params)
     });
     const data = await resp.json ();
     if (data.resCode! == "0") {
       throw data;
     }
     return resp.headers.get ("reset-password-token");
   }

I want to call the verify function directly in the deno test code. I don't want to modify the verify function because there are many similar functions of this kind.

@ry
Copy link
Member

ry commented Mar 21, 2020

@saibing Sure, I appreciate the goal, but where should deno get the response for "/baas/auth/v1.0/resetpassword/step?step=verify" ? There's no obvious choice for where to send the request to.

@igotfr
Copy link

igotfr commented Sep 1, 2020

@saibing Sure, I appreciate the goal, but where should deno get the response for "/baas/auth/v1.0/resetpassword/step?step=verify" ? There's no obvious choice for where to send the request to.

const response = await fetch("./instagram.json")
.then(dados => dados.json());

console.log(response);

error: Uncaught URIError: relative URL without a base
at unwrapResponse (rt/10_dispatch_json.js:24:13)
at sendAsync (rt/10_dispatch_json.js:75:12)
at async fetch (rt/26_fetch.js:279:29)
at async file:///home/notecomm/testes/fetchDeno.ts:1:18

@nayeemrmn
Copy link
Collaborator

Forgot about this issue. Update: this is answered and blocked by #4981. Any relative URL (an origin-less absolute path is still a relative URL) should be relative to globalThis.location, which we don't have yet.

@stale
Copy link

stale bot commented Jan 6, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 6, 2021
@stale stale bot closed this as completed Jan 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants