2

I've been trying to make a post request using the Postman API however I keep getting a CORS Policy Error. I can make a GET request to the API but I have really been struggling to make the POST request

How i make the call

    var myHeaders = new Headers();

myHeaders.append("X-Api-Key", "The relevant api key");

myHeaders.append("Content-Type", "application/json");



var raw = {

    collection: {

      variables: [],

      info: {

        name: "Sample Collection",

        description: "This is just a sample collection.",

        schema: "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"

      },

      item: [

        {

          name: "This is a folder",

          description: "",

          item: [

            {

              name: "Sample POST Request",

              request: {

                url: "echo.getpostman.com/post",

                method: "POST",

                header: [

                  {

                    key: "Content-Type",

                    value: "application/json",

                    description: ""

                  }

                ],

                body: {

                  mode: "raw",

                  raw: "{\n\t\"data\": \"123\"\n}"

                },

                description: "This is a sample POST Request"

              },

              response: []

            }

          ]

        },

        {

          name: "Sample GET Request",

          request: {

            url: "echo.getpostman.com/get",

            method: "GET",

            header: [],

            body: {

              mode: "formdata",

              formdata: []

            },

            description: "This is a sample GET Request"

          },

          response: []

        }

      ]

    }

}



var requestOptions = {

  method: 'POST',

  mode: 'cors',

  headers: myHeaders,

  body: raw,

  redirect: 'follow'

};



fetch("https://api.getpostman.com/collections", requestOptions)

  .then(response => response.text())

  .then(result => console.log(result))

  .catch(error => console.log('error', error));


})

The response back

Access to fetch at 'https://api.getpostman.com/collections' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I am able to make the call through Postman and works fine, just when doing it through the JavaScript I seem to be getting this error and i'm not sure why.

Any help would be great

bradley plater
  • 1,272
  • 2
  • 9
  • 16
  • Make sure the server has added support for POST method like this: `Access-Control-Allow-Methods: POST, GET, OPTIONS`; in its configuration – pixlboy Mar 31 '20 at 11:10
  • Well I would imagine that it does as Postman isn't a server that I made its an application. Which if they had an api which allows you to post i would assume it would let you post in your requests – bradley plater Mar 31 '20 at 11:18
  • I'm not **too** familiar with Postman's APIs. Are you trying to access the API for configuring collections or a mock API you've already created some other way? – Quentin Mar 31 '20 at 11:22
  • So I am using the API to create a collection of API's. https://docs.api.getpostman.com/?version=latest – bradley plater Mar 31 '20 at 11:55
  • @bradleyplater — What makes you think that Postman intends for that API to be called from browser-side JS? – Quentin Mar 31 '20 at 12:40

0 Answers0