1

I am aware that normally it should not be possible to send content through a GET request ! I am however working on a piece of software for a client where the existing (in production) web rest service of their application exposes the following method :

[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[JSONPBehaviorAttribute(callback = "callback")]       
CrmDataObject Connection(CrmDataObject crmData)

I think that the Method should be "POST", but is is GET and there is not much I can do about this at this stage since this code is in production and there are several third party softwares that are calling this function.

I, however, tried to write a simple client to call this method and keep failing for obvious reasons :

    var req = HttpWebRequest.Create(url);
    req.Method = "GET";
    req.ContentType = "application/json";
    byte[] bytes = UTF8Encoding.UTF8.GetBytes(s);

    req.ContentLength = bytes.Length;

    using (var stream = req.GetRequestStream())
    {
       stream.Write(bytes, 0, bytes.Length);
    }

I am getting "Cannot send a content-body with this verb-type", logically.

How come are 3rd party apps able to call this method and pass a json parameter ? Is it possible to do it in .Net ?

Thanks,

ElRosbif
  • 353
  • 1
  • 6
  • 18

2 Answers2

1

GET specifically doesn't allow for a request body which is why you are getting "Cannot send a content-body with this verb-type".

You typically pass GET parameters through the URL, generally in the query string (eg "path/to/page?param1=value1&param2=value2"). It's been a while since I did WCF, but I think this will actually work if the properties from your type (CrmDataObject) match the query string -- though I would have thought you'd have to have BodyStyle = WebMessageBodyStyle.WrappedRequest.

If you have existing client code that works, ideally you could make a request while you have a debugger attached with a breakpoint at the beginning of this function, and then you could see the original request URL (in the Request object) as well as what was populated into crmData.

var req = HttpWebRequest.Create(url + "?name=value1&name2=value2");
gregmac
  • 24,276
  • 10
  • 87
  • 118
  • Well, technically, GET requests can have bodies, but it appears .NET does not support generating GET requests with bodies. https://stackoverflow.com/questions/2064281/sending-post-data-with-get-request-valid I wonder how the 3rd party apps mentionned above manage call this method though... Since the service explicitely requests JSON, it's problably to happening through Query strings... – ElRosbif Dec 10 '18 at 15:06
  • I don't have the source of the working client code unfortunately :( – ElRosbif Dec 10 '18 at 15:08
0

Depite the fact there's a lot of information needed to make a better suggestion I think your problem might be with the parameter binding

¿Have you tried passing the parameters within the URL? like:

www.myurl.com/Connection?param1=hello&param2=world

Take in consideration that the name of each parameter in the URL must match a property in the CrmDataObject to be parsed by the model binder.

Edgardo
  • 29
  • 4
  • This would conflict with the fact that the webservice expects JSON would it not ? – ElRosbif Dec 10 '18 at 15:02
  • The model binder works for every request type, you can send XML and use the model binder to parse it but you'll have to set ContentType=application/xml inside the request to make it work. If you already tried passing the parameters in the body with no success there's no other option. Give it a try... – Edgardo Dec 10 '18 at 15:44