question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

On Http Post request HttpClient X-XSRF-Token are not setting in an Angular 6

See original GitHub issue
import {HttpClientModule, HttpClientXsrfModule} from "@angular/common/http";

imports: [
    ...
    HttpClientModule,
    HttpClientXsrfModule.withOptions({
      cookieName: 'XSRF-TOKEN',
      headerName: 'X-CSRF-TOKEN'
    })
  ],
...
import {HttpClient, HttpClientModule, HttpClientXsrfModule} from "@angular/common/http";

constructor(private _http:HttpClient) { }

  addUser(info){
    return this._http.post(this.apiURL, info, {
      headers:  { 'Content-Type': 'application/x-www-form-urlencoded' },
      withCredentials: true,
      responseType: "arraybuffer",
    }
 }

The default interceptor created behind the scene by HttpClientXsrfModule does not seem to handle absolute urls…

So I’ve changed the apiURL = “http://localhost/simple_api/insert.php”; TO apiURL = “//localhost/simple_api/insert.php”;

Working Fine. But unable var_dump or print the headers attached in the PHP script. But I’m getting the form data submitted successfully.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
nishanth6commented, Sep 19, 2018

@benlesh @manekinekko

Click Here

Here is my complete code. As I’m new to run this code

From the usr.service.ts it was consoling all the headers after posting the data Headers should be posted or printed with XSRF Token on the console. But it’s not going to happen?

I have a screen Shot of console Click

0reactions
angular-automatic-lock-bot[bot]commented, Sep 14, 2019

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 6 does not add X-XSRF-TOKEN header to http request
The problem once again is Angular's poor documentation. The fact is, Angular will add the X-XSRF-TOKEN header only if the XSRF-TOKEN cookie ...
Read more >
HttpClientXsrfModule - Angular
Configures XSRF protection support for outgoing requests. ... the default cookie name is XSRF-TOKEN and the default header name is X-XSRF-TOKEN .
Read more >
Angular CSRF Protection Guide: Examples and How to Enable It
Since our application server is sending us a token named XSRF-TOKEN, we'll use Angular's HttpClientXsrfModule to protect every outgoing request ...
Read more >
Fix "Invalid CSRF token" error – add the XSRF-TOKEN header ...
Angular provides a built-in support for sending requests secured with the XSRF-TOKEN header. However, it won't add the token to absolute ...
Read more >
Angular 6 does not add X-XSRF-TOKEN header to http request
this.http .post<any>('api/login', {'_username': username, '_pass': password}) .subscribe(/* handler here */);. The post request ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found