WynDiesel
WynDiesel

Reputation: 1214

Cant pass parameter to API

I'm running a REST API service that has this action :

    [HttpPost]
    public FooResponse DoFoo(FooRequest request)
    //public FooResponse DoFoo([FromBody] FooRequest request)
    {
        return null;
    }

My request:

public class FooRequest
{
    public string FooId;
}

I have an Angular client, that's making this call :

startFoo(fooId: string)
{
const url = `${this.baseUrl}StartFoo`;
const params = new HttpParams()
.set('FooId', fooId);
console.log(`params : ${params}`);

const result = this.httpClient.post<fooItem>(url, {params}).toPromise();
return result;
}

When I make the call from PostMan, the FooId is populated, when I call it from Angular, the endpoint is hit, but the param is always null. When I look in the console log, the parameters is there.

I've tried this solution, but it did not resolve my issue.

What am I missing?

Upvotes: 2

Views: 975

Answers (1)

MesutAtasoy
MesutAtasoy

Reputation: 822

You should add [FromBody] attribute in method .

  [HttpPost]
  public FooResponse DoFoo([FromBody] FooRequest request)
    {
        return null;
    }

While you send the request to api, your request body must be in json format.

var fooRequest = { FooId : 1};


const result = this.httpClient.post<fooItem>(url, JSON.stringify(fooRequest) ).toPromise();

I did not try, I guess that It will work.

Upvotes: 4

Related Questions