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,

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

2 Comments

Well, technically, GET requests can have bodies, but it appears .NET does not support generating GET requests with bodies. stackoverflow.com/questions/2064281/… 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...
I don't have the source of the working client code unfortunately :(
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.

2 Comments

This would conflict with the fact that the webservice expects JSON would it not ?
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.