0

Im testing my post via Postman and I get data,when I post the following url(i add the ip hardcoded):

http://localhost:59628/api/AcessInfo/?id=192.168.10.801

but in angular side when I try the same thing,i get id=null in .netcore

url: http://localhost:59628/api/AcessInfo/
 this.http.post(this.url,{params:{id:x} });

my controller:

       public IActionResult turbineDvic(string id)
        {


            var x = _acess.Devices(id);



            return Ok(x);

        }

I tried :

([FromBody]string id)

but that gives my error 405! which I never seen, any idea?seems i ca not add the parameter to my url

moris62
  • 983
  • 1
  • 14
  • 41
  • In postman you are trying query params? is it a GET call or POST call? – Selaka Nanayakkara May 20 '20 at 13:43
  • @SelakaN its post – moris62 May 20 '20 at 13:52
  • So I dont see the IP address part in the url you have provided in angular app? For ex: its just this `http://localhost:59628/api/AcessInfo/` you are missing the full url `http://localhost:59628/api/AcessInfo/?id=192.168.130.101` Check wether if it is the problem and let me know I'll post the answer – Selaka Nanayakkara May 20 '20 at 13:56
  • @SelakaN in my controller i get id=null via angular thats the issue – moris62 May 20 '20 at 14:11
  • Sre you sure it's it an Options request reaching your controller? – David May 20 '20 at 19:48
  • @David it reaches my controller i put a break point,but i get id null! – moris62 May 20 '20 at 20:19
  • I was suggesting that it may be the CORS OPTIONS request hiring your breakpoint. Try posting to `http://localhost:59628/api/AcessInfo/?id=192.168.130.101` directly, without angular params. Does that work? And yet comparing headers from chrome network debugger to postman headers. – David May 20 '20 at 20:56
  • @David yes in that way workd,when i try to post http://localhost:59628/api/AcessInfo/?id=192.168.130.101 i get the ip on the back-end1 – moris62 May 20 '20 at 21:15

1 Answers1

1

The post method for HttpClient needs a body as second parameter. Try passing an empty body

this.http.post(this.url, {}, { params:{id:x} }
David
  • 33,444
  • 11
  • 80
  • 118
  • wow!works and can you tell me what that body could be usually?and why we need it – moris62 May 20 '20 at 22:03
  • The body is for the data that you usually send with http post requests. https://stackoverflow.com/q/16339198/1160794 – David May 21 '20 at 06:26