Skip to content

Problem making 3rd party API calls using SPFx #3086

@simkessy

Description

@simkessy

Category

  • Question

Expected or Desired Behavior

Hey, I was able to make Slack API calls using the class way (CEWP) plus jQuery and $.ajax.
Now I'm trying to learn SPFx and figure out how to make REST calls to 3rd party apis but I'm getting the error below.

Observed Behavior

Access to fetch at 'https://slack.com/api/users.list?token=&channel=' from origin 'https://localhost:4321' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

On the Left side I have my webpart running in the Local workbench, the Right side is the classic example
image

This is my code:

image

    private makeRequest(): Promise<HttpClientResponse> {
        const token =
            "";
        const channel = "";

        const url = `https://slack.com/api/users.list?token=${token}&channel=${channel}`;

        const requestHeaders: Headers = new Headers();

        requestHeaders.append("Content-type", "application/json");
        requestHeaders.append("accept", "application/json");

        const httpClientOptions: IHttpClientOptions = {
            headers: requestHeaders,
            method: "GET"
        };

        return this.context.httpClient
            .get(url, HttpClient.configurations.v1, httpClientOptions)
            .then(response => {
                console.log(response);
                return response.json();
            });
    }

Any help would be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:spfxCategory: SharePoint Framework (not extensions related)type:questionQuestion... if answered, will be tagged as such.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions