c# - REST seems to reject JSON POST -
i'm trying post data in json format server rejecting it. works if post in xml , reply in either xml or json.
the contract is
[servicecontract] public interface iservice { // 1 string data [operationcontract] [webinvoke( bodystyle = webmessagebodystyle.bare, method = "post", responseformat = webmessageformat.xml, uritemplate = "data")] string datapost(hidden param); } // if compilation error, add // system.runtime.serialization references [datacontract(name = "hidden", namespace = "")] public class hidden { [datamember] public string id { get; set; } }
the implementation is
public class service : iservice { public string datapost(hidden param) { console.writeline("datapost " + param.id); return "datapost said " + param.id; } }
on client side, run of mill standard stuff.
namespace client { enum httpverb { get, post, put, delete }; class restclient { // properties public string endpoint { get; set; } public httpverb method { get; set; } public string contenttype { get; set; } public string paramdata { get; set; } public string postdata { get; set; } // methods public string makerequest() { var responsevalue = string.empty; string ep = endpoint; if (!string.isnullorempty(paramdata)) ep += "/" + paramdata; var request = (httpwebrequest)webrequest.create(ep); request.method = method.tostring(); request.contentlength = 0; request.contenttype = contenttype; // postdata parameters if (!string.isnullorempty(postdata) && method == httpverb.post) { var encoding = new utf8encoding(); var bytes = encoding.getencoding("iso-8859-1").getbytes(postdata); request.contentlength = bytes.length; using (var poststream = request.getrequeststream()) { poststream.write(bytes, 0, bytes.length); } if (postdata.substring(0, 1) != "<") request.contenttype = "application/json"; console.writeline("content type " + request.contenttype.tostring()); } // send request , response using (var response = (httpwebresponse)request.getresponse()) { // did work? if (response.statuscode != httpstatuscode.ok) { var message = string.format("request failed. received http {0}", response.statuscode); throw new applicationexception(message); } // response using (var responsestream = response.getresponsestream()) { if (responsestream != null) { using (var reader = new streamreader(responsestream)) { responsevalue = reader.readtoend(); } } } } return responsevalue; } void talkto( httpverb in_method, string in_endpoint, string in_paramdata, string in_postdata) { method = in_method; endpoint = in_endpoint; contenttype = "text/xml"; paramdata = in_paramdata; postdata = in_postdata; try { string response = makerequest(); console.writeline("endpoint: " + endpoint); console.writeline("resp : " + response); console.writeline(); } catch (system.net.webexception e) { console.writeline("endpoint: " + endpoint); console.writeline("failed : " + e.message); } } static void main(string[] args) { restclient me = new restclient(); string endpointprefix = @"http://localhost:8000/"; me.talkto( httpverb.post, endpointprefix + "data", "", "<hidden><id>xml works</id></hidden>"); string post = "{\"id\":\"json works\"}"; console.writeline("json string [" + post + "]"); me.talkto( httpverb.post, endpointprefix + "data", "", post); console.writeline("press <enter> terminate"); console.readline(); } } }
on client side, i'm getting
content type text/xml endpoint: http://localhost:8000/data resp : <string xmlns="http://schemas.microsoft.com/2003/10/serialization/">datapost said xml works</string> json string [{"id":"json works"}] content type application/json endpoint: http://localhost:8000/data failed : remote server returned error: (400) bad request. press <enter> terminate
when @ help, says json format allowable when try it, falls over. trace sever debug seems imply ignoring application/json , assuming xml.
exception: system.runtime.serialization.serializationexception, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089 message: there error checking start element of object of type system.string. data @ root level invalid. line 1, position 1. last few lines of stack trace system.runtime.serialization.xmlobjectserializer.isstartobjecthandleexceptions(xmlreaderdelegator reader) system.runtime.serialization.datacontractserializer.isstartobject(xmldictionaryreader reader) system.servicemodel.dispatcher.singlebodyparametermessageformatter.readobject(message message)
all examples i've looked @ on , other sites don't add special not working because i'm using vs2013 express?
edit happens on vs2013 web express
edit2 doesn't isolated express versions - i've tried on vs2010 professional. still using xmlobjectserializer parse json , failing miserably.
the fix no convert postdata bytes
if (!string.isnullorempty(postdata) && method == httpverb.post) { if (postdata.substring(0, 1) != "<") request.contenttype = "application/json"; console.writeline("content type " + request.contenttype.tostring()); request.contentlength = postdata.length; using (var poststream = new streamwriter(request.getrequeststream())) { poststream.write(postdata); } }
Comments
Post a Comment