Base solution for your next web application
Open Closed

POST method not working with API (ApplicationService) #2450


User avatar
0
alinm created

I have a service in TemplateAppService :

[HttpPost]
public bool PostData([FromBody]ParametersDto _dto)
{
     //some code
     return true;
}

Angular Service(Typescript):

@Injectable()
export class DocumentService {

  private headers: any;
  private heroesUrl = 'http://172.18.0.81:62114/api/services/app/';  // URL to web api

  constructor(private http: Http, private authenticationService: AuthenticationService) {  
  }

  postDocData(ParametersDto data): Promise<any> {
    let headers = new Headers({ 'Content-Type': 'application/json', Authorization: 'Bearer ' + this.authenticationService.token });
    let options = new RequestOptions({ headers: headers });

    return this.http.post(this.heroesUrl + "TemplateDtl/PostData", JSON.stringify({dto:data}), options).toPromise()
      .then(response => response.json().result as any)
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }
}

The post method does not work no matter what! I've tried passin a single parameter like this: [HttpPost]public bool PostData(int id)

I've tried changing request headers and added [FromBody] Attribute to parameters, but cannot get POST method to work. I also included [FromBody] & [HttpPost] Attributes in ITemplateAppService(interface) and TemplateAppService(class), still not working!

All the get methods (default) work fine. Methods other than GET behave strangely. What am I doing wrong? Am I missing something?


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you share ParametersDto both for Typescript and server side ? Thanks.