I
I
Ingvar Von Bjork2021-03-07 19:47:25
Angular
Ingvar Von Bjork, 2021-03-07 19:47:25

How to send curl via Angular?

I'm trying to send curl like this:

curl -X POST https://secure.payu.com/api/v2_1/orders \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer 3e5cac39-7e38-4139-8fd6-30adc06a61bd" \
    -d '{
        "notifyUrl": "https://your.eshop.com/notify",
        "customerIp": "127.0.0.1",
        "merchantPosId": "145227",
        "description": "RTV market",
        "currencyCode": "PLN",
        "totalAmount": "21000",
        "buyer": {
            "email": "[email protected]",
            "phone": "654111654",
            "firstName": "John",
            "lastName": "Doe",
            "language": "pl"
        },
        "products": [
            {
                "name": "Wireless Mouse for Laptop",
                "unitPrice": "15000",
                "quantity": "1"
            },
            {
                "name": "HDMI cable",
                "unitPrice": "6000",
                "quantity": "1"
            }
        ]
    }'


In angular it looks something like this:
const body = {
  "notifyUrl": "https://your.eshop.com/notify",
  "customerIp": "127.0.0.1",
  "merchantPosId": "145227",
  "description": "RTV market",
  "currencyCode": "PLN",
  "totalAmount": "21000",
  "buyer": {
    "email": "[email protected]",
    "phone": "654111654",
    "firstName": "John",
    "lastName": "Doe",
    "language": "pl"
  },
  "products": [
    {
      "name": "Wireless Mouse for Laptop",
      "unitPrice": "15000",
      "quantity": "1"
    },
    {
      "name": "HDMI cable",
      "unitPrice": "6000",
      "quantity": "1"
    }
  ]
}

this.http
  .post<any>('https://secure.snd.payu.com/api/v2_1/orders', body, { headers: headers })
  .subscribe(res => {
    console.log(res);
  });

The request from the console works fine, but the request from the browser returns an error
Access to XMLHttpRequest at ' https://secure.snd.payu.com/api/v2_1/orders ' from origin ' localhost:4200 ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Antonio Solo, 2021-03-07
@solotony

the default browser checks whether the current site is allowed to make requests to a specific server (CORS policy).
to get around this, you must either add CORS support for the server or disable CORS in the browser

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question