2

I have an Angular service with this Put request method:

put(peopleFieldID: number, peopleFieldName: string): Observable<number> {
let params = new HttpParams();
params = params.append("peopleFieldID", peopleFieldID.toString());
params = params.append("peopleFieldName", peopleFieldName);

return this.http
  .put<number>(this.url, { params: params })
  .pipe(catchError(this._errorHandling.handleError.bind(this)));}

The above hits this endpoint on my .net core API:

    [Authorize(Roles = "Client")]
    [Route("")]
    [HttpPut]
    public IActionResult C_Update(int peopleFieldID, string peopleFieldName )
    {
        //Make call to the proper repo method.
        return Json(_peopleFieldRepo.C_Update(peopleFieldID, peopleFieldName));
    }

The two params, peopleFieldID and peopleFieldName, are always 0 and null. I have pinpointed that the Angular frontend sends the params correctly, but the backend cannot recognize them. I have many other endpoints where this works just fine.

1 Answer 1

7

You need to set query params using HttpParams and pass null for payload if you do not have any:

const params = new HttpParams()
            .set('peopleFieldID', peopleFieldID.toString())
            .set('peopleFieldName', peopleFieldName);
// if you have a payload to pass you can do that in place of null below
return this.http
  .put<number>(this.url, null, { params: params })
  .pipe(catchError(this._errorHandling.handleError.bind(this)));}
2
  • This did solve the issue. I thought because the body param was optional, that it wouldn't matter if I did not include it. I don't include it on Get requests, but it makes sense why I would need it on Post/Puts. Thank you!
    – CAlex
    Mar 28, 2019 at 2:47
  • 1
    Yeah, you don't need the body for Get but Put and POST need it , so send null if you don't have any need.
    – nircraft
    Mar 28, 2019 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.