Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - JSON Put and Delete Returns 403 (But postman app works well)

I have an rest api which i could use post and get requests. But when i want to use put and delete request it returns error 403. But also when i try to put and delete with Postman app (an app for json requests) all is works well. Im really confused. Let me prove the problem with some screenshots. (Im censored links for security, sorry for that)


Chrome console; enter image description here Postman App; enter image description here


Any my put code;

  /** PUT: update the firm on the server */
updateFirm (firm: Firm): Observable<any> {
 console.log(firm);

 return this.http.put(this.firmUrl+"put/"+String(firm.id), firm, httpOptions).pipe(
   tap(_ => console.log(`updated firm id=${firm.id}`)),
   catchError(this.handleError<any>('updateFirm'))
 );
}

I will be very appreciated if you could help me. Have nice day

like image 731
Cem Kocagöz Avatar asked Jan 13 '19 10:01

Cem Kocagöz


People also ask

How do I fix 403 Forbidden in Postman?

The simple answer is; “You need to be given the correct access”. Without being given the correct access you'd technically be hacking the server, as it is specifically set up to restrict said access.

How do I fix REST API 403 Forbidden?

The most common cause of a 403 Forbidden Error is simply inputting an incorrect URL. As discussed before, many tightly secured web servers disallow access to improper URLs. This could be anything from accessing a file directory to accessing a private page meant for other users.

Why do I get an HTTP 403 Forbidden error when connecting to my API gateway APIs from a VPC?

The HTTP 403 Forbidden error most commonly occurs when private DNS is enabled for an API Gateway interface VPC endpoint that's associated with a VPC. In this scenario, all requests from the VPC to API Gateway APIs resolve to that interface VPC endpoint.

When trying to access a URL We got server Error 403 What could be the reason for the message Mcq?

The 403 error is one of the many hypertext transfer protocol responses that can appear when attempting to access a web page. If the '403 Forbidden' message appears in your browser instead of the website you requested, it means that you are not authorized to access the specified URL.


2 Answers

I had a similar situation and found out that Angular 2 performs a OPTIONS method before a POST is done! OPTIONS was missing in "Access-Control-Allow-Methods".

If I check your log, then I think that you should also allow OPTIONS: With angular, and perl as a backend, I had to allow the following:

-"Access-Control-Allow-Methods" => 'GET,POST,PATCH,DELETE,PUT,OPTIONS',

like image 162
Rob Lassche Avatar answered Oct 29 '22 18:10

Rob Lassche


Proxy solves your problem. A nice and fast solution could be to borrow one (like https://cors-anywhere.herokuapp.com) to test then do your own. Here is an example :

import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Component({
  selector: 'app-analyzer',
  templateUrl: './analyzer.component.html',
  styleUrls: ['./analyzer.component.scss']
})
export class AnalyzerComponent implements OnInit {

  res;

  constructor(private http: HttpClient) {
    this.getDeck().subscribe(res => {console.log(res); this.res = res; });
  }

  ngOnInit() {
  }

  getDeck() {
    const headers = new HttpHeaders({
      'X-Requested-With': 'XMLHttpRequest'
    });
    return this.http.get('https://cors-anywhere.herokuapp.com/https://www.keyforgegame.com/api/decks/30763530-041c-4e15-b506-3456e79141d2/',
      {headers: headers}
      );
  }

}

enter image description here

like image 43
Leasye Avatar answered Oct 29 '22 20:10

Leasye