how to handle special character( in values) of matrix parameter - Spring REST URL - spring-restcontroller

I need to pass itemNumber1 value as 075/458
http://localhost:8080/projectroot/some/itemNumber=075%2F458
or
http://localhost:8080/projectroot/some/itemNumber=075/458
But this is not hitting my controller method:
#RequestMapping("/some/{number}")
public #ResponseBody void getSomething(
#MatrixVariable(required = true) String itemNumber1,
#MatrixVariable(required = false) String itemNumber2,
#MatrixVariable(required = false) String itemNumber3)

I see that you are trying to parse it via the URL, but you are trying to get it by #ResponseBody. If you are using GET method to parse the value, your response body will be empty. If you try to get the data via response body, try the POST method instead.

Related

How can I get the value from #QueryParam

Basically what I'm trying to do is edit an entity that's stored in my db. For that I have a little method that is trying to access a paramater that's defined with #PathParam. My problem is that it comes back as null. Here is my method:
#PUT
#Path("/{id}")
#Produces(MediaType.APPLICATION_JSON)
public Response edit(#PathParam("id") Long id, #QueryParam("myParam") String name)
{
return Response.ok().build();
}
I`m using Postman to send the parameter to my server. My URL looks like this:
http://localhost:8080/myApplication/rest/users/1?myParam=test
How can I get the value from the parameter?
Try with
public Response edit(#PathParam("id") Long id, #QueryParam("myParam") String myParam) {
return Response.ok().build();
}
This will work. The query param and variable name will get auto bind if they are similar.

Web API2 read request body

I have a web API2 item that I need to read the 'body' of the incoming request. The client is sending information (via 'PUT') in the body opposed to parameters in the URL. I have been searching for a solution but keep hitting a wall. Can anyone advise how I can get this body text?
Thanks
<HttpOptions>
<Route("v1/cth/test"), AcceptVerbs("PUT", "POST", "OPTIONS")>
Public Function CTHInterface(ByVal passedjson As Object) As String
Return "Hello"
End Function
FYI, I finally managed to get to the body. Hope this helps someone else.
<HttpOptions>
<Route("v1/cth/dev"), AcceptVerbs("PUT", "POST", "OPTIONS")>
Public Function CTHInterfaceDev() As String
Dim CTHStream As New StreamReader(HttpContext.Current.Request.InputStream)
Dim CTHBody As String = CTHStream.ReadToEnd
Return "Hello"
End Function

How can i get html headers with phpunit using Selenium2?

public function testheaders()
{
$url=$this->url('http://www.example.com/index.php');
$kur = get_headers($url);
var_dump($kur);
}
I got the error : get_headers(): Filename cannot be empty.
My class extends PHPUnit_Extensions_Selenium2TestCase like my all my other tests.
get_headers() is a PHP function that will give you the headers return by an HTTP request to a specified url. The parameter you need to give is a string, but $this->url() return an PHPUnit_Selenium-object.
If you want the headers of that known URL, why not do directly?
$kur = get_headers('http://www.example.com/index.php');

Passing a string param to a RESTful service during POST action

I am having a RESTful service with the following method:
[WebInvoke]
string GetDataFromStringAsString(string xmlString);
My client call to the method is as below:
var client = new RestClient();
client.BaseUrl = serviceBaseUrl;
var request = new RestRequest(method){RequestFormat = DataFormat.Xml};
request.Resource = resourceUrl;
request.AddParameter("text/xml", requestBody,
ParameterType.RequestBody);
var response = client.Execute(request);
Let us take a string to post as "Hello World".
Now the string that i post to the above method gives me a 400 Bad
request. In order to get it working i had to wrap the above string in
a element as shown below:
<string xmlns="http://schemas.microsoft.com/2003/10/
Serialization/">Hello World</string>
Now when i post the above string i get a success response back from
the server.
Why is that i have to manually wrap the string to make it work. Is
there a way that i can achieve to post a string without doing the
above manually.
The only other way that I am aware of is to use stream as your input parameter. e.g.
[WebInvoke]
string GetDataFromStringAsString(stream xmlString);
The problem with .Net 4 WCF REST is that fundamentally WCF only knows how to pass two types of info, either XML or a stream of bytes. Personally, I would use WCF Web API instead of the standard WCF REST library because you are going run into lots more of these kinds of issues.

WCF WebInvoke with query string parameters AND a post body

I'm fairly new to web services and especially WCF so bear with me.
I'm writing an API that takes a couple of parameters like username, apikey and some options, but I also need to send it a string which can be a few thousands words, which gets manipulated and passed back as a stream. It didn't make sense to just put it in the query string, so I thought I would just have the message body POSTed to the service.
There doesn't seem to be an easy way to do this...
My operation contract looks like this
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate="Method1?email={email}&apikey={apikey}"+
"&text={text}&quality={qual}", BodyStyle = WebMessageBodyStyle.Bare)]
Stream Method1(string email, string apikey, string text, string qual);
And this works. But it is the 'text' parameter I want to pull out and have in the post body. One thing I read said to have a stream as another parameter, like this:
Stream Method1(string email, string apikey, string qual, Stream text);
which I could then read in. But that throws an error saying that if I want to have a stream parameter, that it has to be the only parameter.
So how can I achieve what I am trying to do here, or is it no big deal to send up a few thousand words in the query string?
https://social.msdn.microsoft.com/Forums/vstudio/en-US/e2d074aa-c3a6-4e78-bd88-0b9d24b561d1/how-to-declare-post-parameters-in-wcf-rest-contract?forum=wcf
Best answer I could find that tackles this issue and worked for me so I could adhere to the RESTful standards correctly
A workaround is to not declare the query parameters within the method signature and just manually extract them from the raw uri.
Dictionary<string, string> queryParameters = WcfUtils.QueryParameters();
queryParameters.TryGetValue("email", out string email);
// (Inside WcfUtils):
public static Dictionary<string, string> QueryParameters()
{
// raw url including the query parameters
string uri = WebOperationContext.Current.IncomingRequest.UriTemplateMatch;
return uri.Split('?')
.Skip(1)
.SelectMany(s => s.Split('&'))
.Select(pv => pv.Split('='))
.Where(pv => pv.Length == 2)
.ToDictionary(pv => pv[0], pv => pv[1].TrimSingleQuotes());
}
// (Inside string extension methods)
public static string TrimSingleQuotes(this string s)
{
return (s != null && s.Length >= 2 && s[0] == '\'' && s[s.Length - 1] == '\'')
? s.Substring(1, s.Length - 2).Replace("''", "'")
: s;
}
Ended up solving simply by using WebServiceHostFactory