Skip to content

403 Forbidden - Using in browser via browserify #1

@Deftwun

Description

@Deftwun

Here's my basic test script bundled with browserify & loaded on client side browser

var ws_api_client = require('ws-api-client-nodejs');
var defaultClient = ws_api_client.ApiClient.instance;

// Configure API key authorization: user_key
var user_key = defaultClient.authentications['user_key'];
user_key.apiKey = "*****"

var api = new ws_api_client.MakesApi()

var opts = { 
  'countries': "us" // {String} Show information for local manufacturers from specified countries only. Use `GET /countries/` method to get the full list of countries. (e.g. `us,gb,jp`)
};

var callback = function(error, data, response) {
  if (error) {
    console.error(error);
  } else {
    console.log('API called successfully. Returned data: ' + JSON.stringify(data, null, 2));
  }
};
api.makesList(opts, callback);

and console output with 403 error:
image

Looks like superagent is setting content type header to application/json which causes browser to do a preflight OPTION request, turning this into a CORS issue.

Changing this line to request.type("text/plain"); seems to fix the problem (it becomes a 'simple' request)

Or is this a sign that I shouldn't be using this on client side?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions