Search code examples
javascriptjavascript-objectsresponse

URL is empty when defining a Response object


I am trying to create a Response request like that:

Response { type: "basic", url: "https://mywebsite.com/download?lang=en", status: 200, ok: true, statusText: "SuperSmashingGreat!"}

I read here how to set a new Response object, and I tried:

var myBlob = new Blob();

var init = {
  "status": 200,
  "statusText": "SuperSmashingGreat!",
  "url": "https://mywebsite.com/download?lang=en",
  "ok": "true"
};

var myResponse = new Response(myBlob, init);

console.log(myResponse)

After I run it, I received this Response object without the url (it is empty url: ""):

{
  type: "default",
  url: "",
  redirected: false,
  status: 200,
  ok: true,
  statusText: "SuperSmashingGreat!",
  headers: Headers,
  body: ReadableStream,
  bodyUsed: false
}

Why it is not being set?


Solution

  • Thanks for @Blair Nangle for the comment, based on this website.
    I used:

    Object.defineProperty(resp, "url", { value: "foobar" });  
    

    Full working code:

    var myBlob = new Blob();
    
    var init = {
      "status": 200,
      "statusText": "SuperSmashingGreat!",
      "url": "https://mywebsite.com/download?lang=en",
      "ok": "true"
    };
    
    var resp = new Response(myBlob, init);
    
    Object.defineProperty(resp, "resp", { value: "https://mywebsite.com/download?lang=en" });
    console.log(myResponse)