Skip to content

All configured HTTP headers are reset when using Upload extension #1489

@P-147

Description

@P-147

Screenshot

Image

Description

When the Upload extension for graffle is used with a file sent in a GraphQL request, headers previously set on the transport() function are reset.
The issue seems to be linked to a destructuring on a Header Object.

Problematic line of code :

...pack.input.transport.headers,

Reproduction Steps

// STEP 1: Prove that destructuring isn't a good approach with a Header Object
const myHeaders = new Headers();
myHeaders.append("Content-Type", "text/xml");
myHeaders.append("Vary", "Accept-Language");

console.log("\n\nLogging headers: ", myHeaders)
console.log("Using destructuring: ", { ...myHeaders })
console.log("Converting to Object from entries: ", Object.fromEntries(myHeaders.entries()))


// STEP 2: Live test: inspect request payload sent @ https://graffle_upload.requestcatcher.com/
import { Graffle } from 'graffle'
import { Upload } from 'graffle/extensions/upload'
import { inspect } from 'node:util';

const graffle = Graffle
  .create()
  .transport({
    url: "https://graffle_upload.requestcatcher.com/",
    headers: {
      Authorization: 'Basic ' + Buffer.from("username:password").toString('base64'),
      'x-custom-header': 'MY_CUSTOM_VALUE',
    },
  })
  .use(Upload)
  .anyware(async ({ encode }) => {
    // Inspect requests contents 
    console.log("\n\nENCODE (before pack hook): ==> ", inspect(encode.input.transport, { depth: 3 }))
    const { pack } = await encode()
    console.log("\n\nPACK (after pack hook): ==> ", inspect(pack.input.transport, { depth: 3 }))
    return await pack()
  })


// Test Request with file
const data = graffle.gql(`
  mutation UploadFile($file: Upload!) {
    uploadFile(file: $file) {
      id
      url
    }
  }
`).$send({
  blob: new Blob([`Hello World: this is a file!`], { type: `text/plain` }),
})

try {
  let res = await data
  console.log("DATA IS :", res)

} catch (error) {
  // Expected error, as ping API will not respond with a JSON.
}

Console Output from test code

Notice that the headers are reset between encode & pack.

Logging headers:  Headers { 'Content-Type': 'text/xml', Vary: 'Accept-Language' }
Using destructuring:  {}
Converting to Object from entries:  { 'content-type': 'text/xml', vary: 'Accept-Language' }


ENCODE (before pack hook): ==>  {
  methodMode: 'post',
  raw: undefined,
  url: 'https://graffle_upload.requestcatcher.com/',
  headers: Headers {
    Authorization: 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=',
    'x-custom-header': 'MY_CUSTOM_VALUE'
  }
}


PACK (after pack hook): ==>  {
  methodMode: 'post',
  raw: undefined,
  url: 'https://graffle_upload.requestcatcher.com/',
  headers: { 'content-type': '' }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions