gothinkster / angular-realworld-example-app

Exemplary real world application built with Angular
MIT License
5.27k stars 3.18k forks source link

Angular application error ,calling back-end api POST /users with Content-Type:text/plain #78

Closed ajitkumargiri closed 6 years ago

ajitkumargiri commented 6 years ago

I am trying in local to register through angular app but not able to call api backend. I debug it and found below error.

Request URL:http://localhost:8080/users Request Method:POST Status Code:415 Remote Address:[::1]:8080 Referrer Policy:no-referrer-when-downgrade

Accept:application/json, text/plain, / Accept-Encoding:gzip, deflate, br Accept-Language:en-US,en;q=0.9 Connection:keep-alive Content-Length:64 Content-Type:text/plain Host:localhost:8080 Origin:http://localhost:4200 Referer:http://localhost:4200/register User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36

qdelance commented 6 years ago

You may have to set proper HTTP headers. Current course is based on Angular2, so you may find some code in the "m-X" branches that deal with it in api service. A better way to do it has been recently merged in master, that is based on newer http client module, and interceptors. More info here: https://github.com/gothinkster/angular-realworld-example-app/commit/96277cb515c573ffb4f62775e7d6ebf9f3661ae4

dmytrohridin commented 6 years ago

https://github.com/gothinkster/angular-realworld-example-app/commit/96277cb515c573ffb4f62775e7d6ebf9f3661ae4 I think there is error. Because of

const headersConfig = {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    };

const token = this.jwtService.getToken();
if (this.jwtService.getToken()) {
  headersConfig['Authorization'] = 'Token ${this.jwtService.getToken()}';
  const authReq = req.clone({ setHeaders: headersConfig });
  return next.handle(authReq);
 }

so only if you will get token - headers will be set. But you do not have any token when you need to register user. I provide PR that fix this issue https://github.com/gothinkster/angular-realworld-example-app/pull/80

dmytrohridin commented 6 years ago

@ajitbapina you can close this issue, changes merged to master

Toxicable commented 6 years ago

closed via https://github.com/gothinkster/angular-realworld-example-app/pull/80