nodeSolidServer/solid-auth-client

Support Request and Headers objects as parameter

jason076 opened this issue · 7 comments

Hi, I am trying to create a container in a solid pod using the the fetch() method of the solid-auth-client. I create the POST request as follows:

public createContainer(url: string, name: string): void {
        const reqHeader = new Headers();
        reqHeader.append('Host', 'example.org');
        reqHeader.append('Content-Type', 'text/turtle');
        reqHeader.append(
            'Link',
            '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"'
        );
        reqHeader.append('Slug', name);
        const req = new Request(url, {
            method: 'POST',
            headers: reqHeader,
            body: '<> <http://purl.org/dc/terms/title> "Basic container" .'
        });

        this.authService.fetch(req).then(res => {
            if (!res.ok) {
                throw new SolidFailedCreatingContainer(url, name);
            }
        });
    }

The call to this.authService.fetch() is just a wrapper for window.solid.auth.fetch(). I'm using solid-auth-client v 2.3.1.

The call from above results into the following header:

POST /your-companion HTTP/1.1
Host: jason07.localhost:8443
Connection: keep-alive
Content-Length: 55
Pragma: no-cache
Cache-Control: no-cache
authorization: Bearer 
<TOKEN_OMITTED>
Origin: http://localhost:8100
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36
Accept: */*
Sec-Fetch-Site: cross-site
Sec-Fetch-Mode: cors
Referer: http://localhost:8100/negative-thoughts
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: <COOKIE_OMITTED>

All header fields that I have added to the request are missing.

The Host Header, you’re not allowed to set (by the browser). The others should work though. Can you omit Host, and first test whether it works with regular fetch?

The Host Header, you’re not allowed to set (by the browser). The others should work though. Can you omit Host, and first test whether it works with regular fetch?

If I omit Host it still ignores my header, but it works with a regular fetch.

Thanks for reporting, fixed in v2.4.0.

Thanks for reporting, fixed in v2.4.0.

I have updated the package to v2.4.0, but it is still the same problem.

Did you allow to pass a Headers object in the RequestInit object? I ask this, because the new issue title does suggest it. I don't pass a RequestInit object. I pass a whole Request object to fetch()

If I pass a URI and a RequestInit object instead of a Request object the fetch is working:

 this.authService
            .fetch(url, {
                method: 'POST',
                body: '<> <http://purl.org/dc/terms/title> "Basic container" .',
                headers: reqHeader
            })
            .then(res => {
                if (!res.ok) {
                    throw new SolidFailedCreatingContainer(url, name);
                }
            });

Weird, I had tested your example successfully; headers got added.

Can you change this into a minimal working example that only uses solid-auth-client (not authService)?

const reqHeader = new Headers();
reqHeader.append('Content-Type', 'text/turtle');
reqHeader.append(
            'Link',
            '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"'
        );
reqHeader.append('Slug', name);
const req = new Request(url, {
            method: 'POST',
            headers: reqHeader,
            body: '<> <http://purl.org/dc/terms/title> "Basic container" .'
        });
solid.auth.fetch(req);