Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I use RestSharp to test my service.
C#
[OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/GenerateMetadata/{FacilityID}")]
        public List<dynamic> GenerateMetadata(string FacilityID)
        {

And in the test code:
C#
var endpoint = String.Format("http://localhost/blahblah.svc/GenerateMetadata/FacilityID={0}", "555");
            var client = new RestClient(endpoint);
            var request = new RestRequest(endpoint, Method.POST);
            request.RequestFormat = DataFormat.Json;
            var response = client.Execute(request);
            dynamic data = JsonConvert.DeserializeObject<dynamic>(response.Content);


But I got an error "Bad request", why?
The corresponding reference is here.
Posted
Updated 20-Apr-15 5:07am
v2
Comments
Richard C Bishop 20-Apr-15 16:57pm    
It does not appear that you are passing your parameter correctly. You don't have it set up as a query string. See below.
[no name] 20-Apr-15 17:09pm    
I added the header, but it is still wrong. When I hover over the response node, it showed

ResponseUri: /blahblah.svc/GenerateMetadata/FacilityID=555/http://localhost/blahblah.svc/GenerateMetadata/FacilityID=555

Not sure....
Richard C Bishop 20-Apr-15 17:11pm    
Please see my solution and let me know if that helps. You seem to be missing the "?" that signifies a query string parameter.
[no name] 21-Apr-15 9:16am    
When I typed http://localhost/blahblah.svc/ in the browser then hot enter, it shows "Endpoint not found". What could be the killer? I used IIS 7.5. I ping it from the cmd and still nothing.
Rajat_RJT 23-Apr-15 0:50am    
try with this url - "http://localhost/blahblah.svc" , then let me know whatever error comes.

var endpoint = String.Format("http://localhost/blahblah.svc/GenerateMetadata/?FacilityID={0}", "555");
 
Share this answer
 
v2
Comments
[no name] 20-Apr-15 20:10pm    
I don't know why I got "endpoint no found" error in Response.Context. The StatusCode is "NotFound".
Richard C Bishop 21-Apr-15 8:10am    
I would need more info and code to tell you more. I just know that when calling a REST API you need a "?" to pass the query parameters.
If this parameter is set, its value will be sent as the body of the request. Only one RequestBody Parameter is accepted – the first one.

The name of the parameter will be used as the Content-Type header for the request.

RequestBody does not work on GET or HEAD Requests, as they do not actually send a body.

If you have GetOrPost parameters as well, they will overwrite the RequestBody – RestSharp will not combine them but it will instead throw the RequestBody parameter away.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900