0

I have a C# post method :

[HttpPost("signup")]
public async Task<IActionResult> Signup([FromBody] User user)

using swagger:

request url: https://localhost:7095/Auth/signup

payload:

curl -X 'POST' \
  'https://localhost:7095/Auth/signup' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{
  "userId": 0,
  "firstname": "string",
  "surname": "string",
  "email": "string",
  "sex": "string",
  "dateOfBirth": "2023-04-03T18:32:08.405Z",
  "password": "string",
  "username": "string"
}'

On my Angular 14 side:

I get to my add method:

add<T>(data: any, apiMethod: string): Observable<T> | undefined {

    let callPoint = this.endpoint.concat('/', apiMethod);
    console.log(callPoint);
    try {
      var tt = this.httpClient.post<T>(callPoint, JSON.stringify(data), this.httpHeader);
      return tt;
    } catch (e) {
      console.log(e);
    }
    return;
  }

From the first console.log I can see that My end point matches the endpoint in my curl command, and my headers match. I'm also passing a user payload:

with a breakpoint on return tt I have:

enter image description here

and if I drill down to the headers part in the console window I have:

enter image description here

The call doesn't fail. it never reaches my endpoint. If I go to the network tab in the browser, there is no entry there.

I thought it might be a CORS policy on my WebApi side, but then I should still see a failure and an attempt to reach the endpoint, but I dont. has anybody an idea why I'm not getting out to the end point?

bilpor
  • 3,467
  • 6
  • 32
  • 77
  • Are you doing a case-insensitive mapping of your JSON property names to their C# property? The [default should be case-insensitive](https://stackoverflow.com/a/70476556/17487348) though. – Mushroomator Apr 03 '23 at 21:06
  • Does the curl/Postman invocation work? – Guru Stron Apr 03 '23 at 21:10
  • @GuruStron yes, if I run swagger and pass the expected values it works. It works if I leave out the UserId as well. – bilpor Apr 03 '23 at 21:19
  • @Mushroomator I changed the case so that they match on the payload, still didn't reach the end point – bilpor Apr 03 '23 at 21:20

2 Answers2

0

You need to subscribe to your http post call before the call will start. Did you subscribed to your add function? https://angular.io/guide/http#starting-the-request

Wen W
  • 2,434
  • 1
  • 10
  • 15
  • I haven't subscribed. If I subscribe though, doesn't it stop the call being async – bilpor Apr 04 '23 at 06:26
  • No, you don't subscribe in your `add` function. you subscribe the to the `add` function in your `component.ts` file. here's an example: https://stackblitz.com/edit/angular-api-call?file=src%2Fapp%2Fapp.component.ts – Wen W Apr 04 '23 at 11:35
0

you have to use subscribe with the add methode

this._myService.add().subscribe()

Lajil Adel
  • 50
  • 2
  • 8