0

I'm trying send a request in Ionic using @angular/http

By side on the server (nodejs) I'm print the req.header

When request is via PHP/Postman/curl/Ionic Server Proxy added, the header is:

{
        "accept": "*/*",
        "accept-encoding": "gzip, deflate",
        "access_token": "foo",
        "cache-control": "no-cache",
        "connection": "keep-alive",
        "content-length": "0",
        "content-type": "application/json",
        "host": "localhost:8081",
        "login": "11111111111",
        "postman-token": "72260d7c-6bd8-449a-95a7-290bf44bc61d",
        "senha": "1234",
        "user-agent": "PostmanRuntime/7.3.0"
    }

But when I send request using Ionic Http request the header is:

{
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "accept-encoding": "gzip, deflate",
        "accept-language": "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
        "access-control-request-headers": "access_token,content-type,login,senha",
        "access-control-request-method": "POST",
        "cache-control": "no-cache",
        "connection": "keep-alive",
        "host": "localhost:8081",
        "origin": "http://localhost:8100",
        "pragma": "no-cache",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:62.0) Gecko/20100101 Firefox/62.0"
    }

Why the very differences? My server it's work with first header.

Anybody helper me?

PHP REQUEST

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://localhost:8081/api/card/login/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => array(
    "Cache-Control: no-cache",
    "Content-Type: application/json",
    "access_token: foo",
    "login: 11111111111",
    "senha: 1234"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

IONIC REQUEST

var headers = new Headers({
            'Content-Type': 'application/json',
            'senha': '1234',
            'login': '11111111111',
            'access_token': 'foo'
        })

        let options = new RequestOptions({
            headers: headers,
            method: RequestMethod.Post,
            withCredentials: true
        })

        this.httpClient.post("http://localhost:8081/api/card/login/", {}, options).subscribe(data => {
            console.log(data['_body'])
        }, error => {
            console.error('API Error : ', error.status)
            console.error('API Error : ', JSON.stringify(error))
        })
  • Looks like Ionic is generating a [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Functional_overview) preflight request. Have you also checked the method? I think you’ll find it’s actually `OPTION` from Ionic. – msbit Sep 20 '18 at 22:15
  • Yes, the method received on nodejs is ```OPTION``` and the request is a generate a cross domain. If I build Ionic and get www dir, put inside nodejs and run, the header it's work. but http://localhost:8100 (Ionic server) to http://localhost:8081 (nodejs) it's not working. It's very strange – Rondinelli Morais Sep 21 '18 at 15:34
  • Not really strange, the origin is defined by scheme, host and port, so different ports on `localhost` are different origins and trigger CORS. – msbit Sep 21 '18 at 22:06
  • Anyhow, take a look at https://stackoverflow.com/questions/18310394/no-access-control-allow-origin-node-apache-port-issue – msbit Sep 21 '18 at 22:09

0 Answers0