asp.net web api - WebRequest POST json parameter showing as Null on WebAPI -
i have webrequest executing through windows ce project. able send through , able see hitting webapi. issue when request comes through, parameter null.
web api
[route("")] public ihttpactionresult post([frombody] stopinfo stopinfo) { try { _scannerservice.addstops(stopinfo); return ok(stopinfo); } catch (exception ex) { //return request.createerrorresponse(httpstatuscode.badrequest, ex); return null; } }
webrequest
private void btnupload_click(object sender, eventargs e) { stopinfo s1 = new stopinfo(); s1.contactname = "test"; s1.companyname = "ignite"; s1.city = "katy"; s1.addr1 = "22908 mountain view"; s1.addr2 = "suite 300"; s1.state = "tx"; s1.zip = "77449"; string uploadurl = txtservertext.text + "/api/stops"; string json = jsonconvert.serializeobject(s1); system.net.webrequest req = system.net.webrequest.create(uploadurl); req.contenttype = "application/json"; req.method = "post"; byte[] bytes = system.text.encoding.ascii.getbytes(json); req.contentlength = bytes.length; system.io.stream os = req.getrequeststream(); os.write(bytes, 0, bytes.length); //push out there os.close(); system.net.webresponse resp = req.getresponse(); system.io.streamreader sr = new system.io.streamreader(resp.getresponsestream()); }
try use cast when creating httpwebrequest object, below example:
httpwebrequest req = (httpwebrequest)webrequest.create((uploadurl.tostring());
also add below properties httpwebrequest object
req.timeout = yourwebrequesttimeoutlimit req.keepalive = false req.accept = "application/json" req.credentials = new networkcredential(username, password)
Comments
Post a Comment