-1

I try to update the identity of a worker on my project, I use HttpClient with a put, working in Angular 6 project and web API 2 on .NET Core. You can see here the request on the front-end side:

updateWorkerIdentity(worker: WorkerRead) : Observable<WorkerRead> {
    const url = 'workerinfo/activeContractId=' + worker.activeContract.id;
    return this.httpClient.put<WorkerRead>(url , JSON.stringify(worker) ); 
}

And at the API side:

[HttpPut("{activeContractId}")]
public async Task<IActionResult> Put([FromRoute] string activeContractId, [FromBody] WorkerRead worker)
{
    var companyId = GetCompanyId();
    var period = GetPeriod();
    var language = GetLanguage();
    var workerInfo = await _workerInfoService.UpdateWorkerIdentity(companyId, activeContractId, language, worker);

    return Ok(workerInfo);
}

the activeContractId coming from the [FromRoute] is well sent but the worker is still null.

The worker sent from the body is well sent as you can see here in the payload:

enter image description here

and the Content-Type of the header is well application/JSON.

Anyone has an idea?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Loon
  • 19
  • 3

1 Answers1

1

Everything on the server side looks OK for a simple endpoint.

However, based on the [HttpPut("{activeContractId}")] route template the request on the client side should be refactored to match the expected template

updateWorkerIdentity(worker: WorkerRead) : Observable<WorkerRead> {
    const url = 'workerinfo/' + worker.activeContract.id;
    return this.httpClient.put<WorkerRead>(url , worker); 
}

I suspect that the httpClient will internally stringify the payload before sending.

The above code assumes the controller is defined

[Route("[controller]")]
public class WorkerInfoController : Controller {

    //...

    //PUT workerinfo/123456
    [HttpPut("{activeContractId}")]
    public async Task<IActionResult> Put([FromRoute] string activeContractId, [FromBody] WorkerRead worker) {
        var companyId = GetCompanyId();
        var period = GetPeriod();
        var language = GetLanguage();
        var workerInfo = await _workerInfoService.UpdateWorkerIdentity(companyId, activeContractId, language, worker);

        return Ok(workerInfo);
    }    
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472