Fetch sending Authorization header in one but not the other?


#1

Hi there,

I’m really inexperienced with APIs hope you can help me out.

I’m using Fetch to reach my endpoints. Had a previous problem with setting the ‘Content-Type’ request header giving my a weird 404 response. Anyways i removed that Content-Type header and then it was working.

Now i want to like ‘conform’ to standards and place my token in the Authorization header, but its happening again! One endpoint can receive the Authorization header and i can do my checking and its good, the other doesn’t even receive it!

Working:

return fetch(API_URL + 'getEvent', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Authorization': 'Bearer ' + generateKey(token)
                // 'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                signature: generateKey(token),
                eToken: token
            })
        })

Not working:

return fetch(API_URL + 'checkInOutMany', {
                method: 'POST',
                credentials: 'include',
                withCredentials: true,
                header: new Headers({
                    'Accept': 'application/json',
                    'Authorization': 'Bearer ' + generateKey(token)
                }),
                body: JSON.stringify({
                    signature: generateKey(token),
                    participants: toSync,
                    eToken: token
                })
            })

Btw when i test them on the dev server its all working fine, but when i put it on the production server, one endpoint works the other doesnt. They are both in the same controller! (this is CodeIgniter btw)

 public function __construct(){
    parent::__construct();
    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization");
    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
  }

  public function index_options() {
    return $this->response(NULL, REST_Controller::HTTP_OK);
  }

  public function getEvent() {
    // echo 'hi';
    $req = $this->verifyRequest();
    if ($req && $req->eToken) {

      $this->load->model('EventQR_model', 'event');
      $participants = $this->event->getEventParticipants($req->eToken);
      $eventDetails = $this->event->getEventDetails($req->eToken);
      
      header('Content-Type: application/json');
      if ($participants && $eventDetails) {
        $eventInfo = array(
          'eventDetails' => $eventDetails,
          'participants' => $participants
        );
        echo json_encode($eventInfo);
      } else {
        echo $this->textMessage('No participants');
      }
    } else {
      echo 'not working';
    }
  }
public function checkInOutMany() {
    $req = $this->verifyRequest();
    if ($req && $req->participants && $req->eToken) {
      
      $this->load->model('EventQR_model', 'event');
      
      $result = array();
      // i just cant write multi updates in 1 query
      // and this way i can grab each participant's results
      foreach ($req->participants as $p) {
        $res = $this->event->checkInOut($p->checkedIn, $p->token, $req->eToken, $p->lastModified, $p->manual);
        $obj = array(
          'token' => $p->token,
          'checkedIn' => $res
        );
        $obj = (object)$obj;
        $result[] = $obj;
      }

      header('Content-Type: application/json');
      echo json_encode($result);
    } else {
      var_dump($this->input->request_headers());
    }
  }

I’ve tried using Postman to proxy and listen to traffic, but as i am so inexperienced when i set the proxy i can surf the internet from my mobile but on the app/expo logs it just keeps saying ‘Network request failed’, so i can’t see anything.

Anyone help me out here? (with any of my problems above)


closed #2

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.