admin管理员组

文章数量:1401632

I want to send a request to a C# service which has a string as its parameter. I wrote an angular service like this:

export class CategoryService {

  constructor(private http: HttpClient) { }
  getCategory(categoryName: string): Observable<Category> {
    console.log(categoryName);
    return this.http.post("/category/GetCategory", { categoryName: categoryName }, { headers: new HttpHeaders({ 'Content-type': 'application/json' }) });
  }

I also tried:

export class CategoryService {

  constructor(private http: HttpClient) { }
  getCategory(categoryName: string): Observable<Category> {
    console.log(categoryName);
    return this.http.post("/category/GetCategory",  categoryName , { headers: new HttpHeaders({ 'Content-type': 'application/json' }) });
  }

But when they both successfully send the request, the parameter is always null. My C# service is:

    [HttpPost]
    public IActionResult GetCategory([FromBody]string categoryName)

I want to send a request to a C# service which has a string as its parameter. I wrote an angular service like this:

export class CategoryService {

  constructor(private http: HttpClient) { }
  getCategory(categoryName: string): Observable<Category> {
    console.log(categoryName);
    return this.http.post("/category/GetCategory", { categoryName: categoryName }, { headers: new HttpHeaders({ 'Content-type': 'application/json' }) });
  }

I also tried:

export class CategoryService {

  constructor(private http: HttpClient) { }
  getCategory(categoryName: string): Observable<Category> {
    console.log(categoryName);
    return this.http.post("/category/GetCategory",  categoryName , { headers: new HttpHeaders({ 'Content-type': 'application/json' }) });
  }

But when they both successfully send the request, the parameter is always null. My C# service is:

    [HttpPost]
    public IActionResult GetCategory([FromBody]string categoryName)
Share Improve this question asked Nov 24, 2018 at 10:20 Nick Mehrdad BabakiNick Mehrdad Babaki 12.6k15 gold badges50 silver badges72 bronze badges 8
  • 1 The first approach is the correct approach, the second one is not. But as you said both of them don't work. In that case could you check if you are able to successfully receive the categoryName when you send it via Postman for eg? – SiddAjmera Commented Nov 24, 2018 at 10:22
  • If I remove [FromBody] I can receive it by Postman but with [FromBody] I cannot – Nick Mehrdad Babaki Commented Nov 24, 2018 at 10:32
  • I'm not so sure about your C# service. I don't even know why [FromBody] is used, but if you're getting it from Postman after removing [FromBody], then with the first approach that you tried, you should also get it from Angular. – SiddAjmera Commented Nov 24, 2018 at 10:34
  • I go to the service but the parameter is always null. – Nick Mehrdad Babaki Commented Nov 24, 2018 at 10:35
  • 2 have you tried using JSON.stringify(categoryName) in your typescript service with FromBody in controller? – Niladri Commented Nov 24, 2018 at 10:59
 |  Show 3 more ments

4 Answers 4

Reset to default 2

I can offer my own solution :)
Angular:

Func(data: string): Observable<any> {
        let params = new HttpParams();
        params = params.set('data', data);
        return this.http.post("url", params);
    }

C#:

public IActionResult Method([FromForm]string data)

Best and easiest solution

this.http.post(this.serverAddress + API_ADDRESS + '?param=' + yourStringParam, null);

I found the solution here: Angular2 - Http POST request parameters I also should remove [FromBody] from my service parameter.

As @Niladri said, this worked for me:

... using JSON.stringify(categoryName) in your typescript service with FromBody in controller

Also this is important:

... keep the content-type as HttpHeaders({ 'Content-Type': 'application/json' })

It works with the FromBody in the controller method :)

本文标签: javascriptAngular HttpClient post cannot send simple string parameterStack Overflow