json - HTTP 415 unsupported media type error when calling Web API 2 endpoint -


i have existing web api 2 service , need modify 1 of methods take custom object parameter, method has 1 parameter simple string coming url. after adding custom object parameter getting 415 unsupported media type error when calling service .net windows app. interestingly, can call method using javascript , jquery ajax method.

the web api 2 service method looks this:

<httppost> <httpget> <route("{view}")> public function getresultswithview(view string, ppaging paging) httpresponsemessage    dim resp new httpresponsemessage    dim lstrfetchxml string = string.empty    dim lstrresults string = string.empty     try       '... work here generate xml string response       '// write xml results response       resp.content = new stringcontent(lstrresults)       resp.content.headers.contenttype.mediatype = "text/xml"       resp.headers.add("status-message", "query executed successfully")       resp.statuscode = httpstatuscode.ok    catch ex exception       resp.statuscode = httpstatuscode.internalservererror       resp.headers.add("status-message", string.format("error while retrieving results view {0}: {1}", view, ex.message))    end try    return resp end function 

the method allows both post , get because paging object optional. if call method get request works.

and simple .net client code calling service looks this:

dim uri string = base_uri + "fetch/someview" dim resp httpwebresponse dim sr streamreader dim lstrresponse string dim reqstream stream dim bytdata byte() dim req httpwebrequest = webrequest.create(uri) dim lstrpagingjson string dim lpaging new paging try    lpaging.page = 1    lpaging.count = 100    lpaging.pagingcookie = ""    req.method = "post"    lstrpagingjson = jsonserializer(of paging)(lpaging)    bytdata = encoding.utf8.getbytes(lstrpagingjson)    req.contentlength = bytdata.length    reqstream = req.getrequeststream()    reqstream.write(bytdata, 0, bytdata.length)    reqstream.close()    req.contenttype = "application/json"     resp = req.getresponse()     sr = new streamreader(resp.getresponsestream, encoding.utf8)    lstrresponse = sr.readtoend    '// response here catch exweb webexception    txtoutput.appendtext("error during request: " + exweb.message) catch ex exception    txtoutput.appendtext(string.format("general error during request {0}: {1}", uri, ex.message)) end try 

the .net client running on 4.5 framework , service on 4.5.2 framework. error thrown @ resp = req.getresponse() line. things tried already:

  • on client, set req.accept value "application/xml" or "text/xml"
  • in service method, removed line `resp.content.headers.contenttype.mediatype = "text/xml"
  • replace xml response content static json, tried rule out problems sending in json on request , getting xml on response

so far keep getting same 415 error response no matter try.

i mentioned works when called javascript, here's ajax call working:

$.ajax({    headers: {},    url: "api/fetch/someview",    type: "post",    data: "{count:100,page:1,pagingcookie:\"\"}",    contenttype: "application/json; charset=utf-8",    datatype: "xml",    success: function (data) {       alert("call succeeded");    },    failure: function (response) {       alert("call failed");    } }); 

on service side, there nothing fancy going on route config or else, it's pretty out-of-the-box web api 2. know routing working, calls being correctly routed method, they're not going somewhere else unexpectedly, missing in .net client? appreciated!

--- update ---
tried create new web api service rule out possible issues existing service, created controller single method takes custom object parameter. tried calling .net client , got same error. tried using webclient instead of httpwebrequest, still same error. worked me web api (prior web api 2).

--- update ---
tried creating new web app using web api 1, when call post complex object parameter coming in null. have web service running web api 1 , verified can still call complex objects. whatever problem is, appears json passing between client , server. have checked json i'm sending , valid, object definition exact match between client , server json should able parsed server.

solved
after banging head on wall couple days issue, looking problem had content type negotiation between client , server. dug deeper using fiddler check request details coming client app, here's screenshot of raw request captured fiddler:

fiddler capture of http request client app

what's missing there content-type header, though setting seen in code sample in original post. thought strange content-type never came through though setting it, had @ other (working) code calling different web api service, difference happened setting req.contenttype property prior writing request body in case. made change new code , did it, content-type showing , got expected success response web service. new code .net client looks this:

req.method = "post" req.contenttype = "application/json" lstrpagingjson = jsonserializer(of paging)(lpaging) bytdata = encoding.utf8.getbytes(lstrpagingjson) req.contentlength = bytdata.length reqstream = req.getrequeststream() reqstream.write(bytdata, 0, bytdata.length) reqstream.close() '// content-type being set here, causing problem 'req.contenttype = "application/json" 

that's was, contenttype property needed set prior writing request body

i believe behavior because once content written body streamed service endpoint being called, other attributes pertaining request need set prior that. please correct me if i'm wrong or if needs more detail.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -