Specifying parameter in PUT - Rest Web Service - rest

I'm building a REST web service using the Jersey API and I've having a problem passing the parameter to the PUT method. The method is this:
#PUT
#Consumes("text/html")
public void putHtml (String content) {
System.out.println("Content"+content);
}
I'm calling it using the following:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function passName()
{
xmlHttp=new XMLHttpRequest();
var url = "/RestWebApp/resources/greeting";
xmlHttp.open("PUT",url,false);
xmlHttp.send();
}
</script>
<title>REST Testing</title>
</head>
<body>
Put name:<input type="text" name="name"/>
<br />
<input type="button" value="Put" onclick="passName()"/>
</body>
</html>
The PUT method is being called since I'm getting "Content " printed in the server console (which is Glassfish) but the parameter is not being read it seems. Is there an #statement or something which I should add to the parameter?
Thanks!
Krt_Malta

You are not setting the body of the request in your client code.
You need to send some content, e.g.
xmlHttp.Send("here is my content")
However you have declared your receiving endpoint to accept "text/html". I would change that to accept "text/plain" instead. Or if you really want to receive multiple values you could accept "application/x-www-urlencoded-form". However you will need to format that yourself in the javascript. Web browsers don't know how to PUT forms. They can only POST forms.

Related

To send Form data to the same page using classic ASP

I want to send Form data to the same page using classic ASP.
Code:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Form</title>
</head>
<body>
<%
'Classic ASP:
Dim test
test = request.form("CtrlTest")
response.write "result = '" & test & "'<br>"
%>
<!-- alternative: <Form name="test" method="POST" target="_self" > -->
<Form name="test" method="POST" action="" >
<a href>ClickThis</a>
<input type="hidden" name="CtrlTest" value="ATest" />
</Form>
</body>
</html>
After clicking I had expected the result to be:
"result='ATest'
ClickThis"
But it is:
"result=' '
ClickThis"
How can I read the posted data?
Tested in localhost mode:
Chrome OK,
IE11: Error 403.14.
This makes me wonder how the POSTing happens:
The POST data is sent to a server that passes the data on to the target Receiving ASP or PHP page.
When the Receiving page opens first time after the transmission the data is available for request.
If the Receiving page is the same as the Sending page (e.g. target =”_self”) the data is immediately available in the page (if it is an ASP or PHP page).
The next time the Receiving page opens the data is no longer available.
Is this correct?
And this should work OK even if I am in localhost mode, and not reaching an actual server?
If the Request lines in the Receiving page lies in a file that is #included into the Receiving file – will it still work?
Links do not submit forms. They just go to the URL in the href attribute.
To submit a form, use a submit button.
<button>Submit</button>

Redirect to GSP failing

I'm using grails 2.4.5
In my grails-app/views directory I have a simple file called "test.gsp". Its contents are trivial GSP/HTML:
<html>
<head>
<meta name="layout" content="main" />
<title>TEST</title>
<style>
</style>
</head>
<body>
<h1>TEST</h1>
</body>
</html>
In a controller action, I have the line:
redirect(uri:"/test.gsp");
But whenever this line is reached, grails sends me off to the 404 handler.
I've tried enabling UrlMapping logging, and it shows:
DEBUG mapping.DefaultUrlMappingsHolder - Matched URI [/test.gsp] with pattern [/(*)/(*)?/(*)?(.(*))?], adding to posibilities
I was sure I had this working previously in Grails. What silly thing am I missing?
You should render a view and not trying to redirect to (probably not mapped) uri.
Try:
render(view: "test")

anchor tag with href as other than http is not working while sending mail to gmail

I am using the smtp sendmail function, in the anchor <a> tag href attribute we have reference other than http:// ie something like below
transauth://some other data
but the gmail is not creating the hyperlink of transauth but creating of http://gmail.com ,Any solutions regarding this.
Gmail strips links that use custom uri schemas.
A work around however is that if you have a website somewhere you can host a simple redirect page that will redirect you to the correct schema.
This is a pretty bullet proof redirect here I copied from this answer to another question
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="1;url=http://example.com">
<script type="text/javascript">
window.location.href = "http://example.com"
</script>
<title>Page Redirection</title>
</head>
<body>
<!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->
If you are not redirected automatically, follow the <a href='http://example.com'>link to example</a>
</body>
</html>

Content-Type for Email Markup Actions

I've been looking at email Actions (which apparently are in a process of being standardized), and considering implementing these for an application of mine.
However, the entire documentation seems to be missing the mime-type to define for the message part that includes this json-ld metadata. For example, gpg signatures are marked as
Content-Type: application/pgp-signature; name="signature.asc"
What Content-Type does this part (eg: this content) need to be included as?
You have to include it in the email’s HTML (text/html).
Google (i.e., Gmail and Inbox by Gmail) supports JSON-LD and Microdata:
The JSON-LD will be included in a script element (used as data block).
The Microdata attributes (like itemscope and itemprop) will be added directly to the (existing) HTML elements.
So, if your email would contain this HTML
<html>
<body>
<p>Foobar</p>
</body>
</html>
you could add JSON-LD to it like this
<html>
<body>
<script type="application/ld+json">
{
"#context": "http://schema.org",
"#type": "Thing",
"name": "Foobar"
}
</script>
<p>Hello!</p>
</body>
</html>
and Microdata like this
<html>
<body itemscope itemtype="http://schema.org/Thing">
<p itemprop="name">Foobar</p>
</body>
</html>

how to mix javascript js file(script embedded with php) and php code in netbeans?

if my hello.js is like this
function jst()
{
var i = 0 ;
i = <?php echo 35; ?>
alert( i );
}
what i really want in netbeans is to interpret that .js file through php interpreter without changing my extension of hello.js to hello.php or in other words i dont wan to change extension of my file from js . the reason behind this is Because netbean provide special support(i.e editing, coloring of text etc) for files with .js extension .
this is how i am including file in index.php
<script>
<?php include 'hello.php'?>;
</script>
code is working fine but i want to use hello.js instead hello.php in netbeans like as shown in following snippet
<script src="hello.js"></script>
what should i do?? how professional websites handle this issue??
*.js
http://s13.postimg.org/vl6vo4fif/image.png
*.php
http://s21.postimg.org/t7tuk42l3/after.png
every thing has converted to plain text after changing extension
You can only do as the most used way, via parameter
hello.js
function jst(alertMe)
{
alert( alertMe );
}
index.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsFileTest</title>
<script type="text/javascript" src="hello.js"></script>
<script type="text/javascript">
var alertMe = <?php echo 35; ?> ;
</script>
</head>
<body>
<button onclick="jst(alertMe)">Try it</button>
</body>
</html>
Develop your js within your php file.
If everything works as expected then you can outsource everything as a separate file .js
But remember : php is parsed and interpreted on server side . So everything outside php tags is completely ignored. Therefore :
<script type="text/javascript" src="jsFile.js"></script>
is pure html and will be ignored. On server side php knows nothing about the existence of these .js file and it will not load and parse it. But this is required if you want php also interpret this file too.
If you want to include it with a php file, you can do it like
Put <script type="text/javascript"> at the beginning. code completion starts again.
jsFile.php
index2.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsFileTest</title>
<?php
include_once 'jsFile.php';
?>
</head>
<body>
<?php
echo "myID = ".$myId."<br>";
?>
<button onclick="myFunction()">Try it</button>
</body>
</html>
Running :
But now we come to the important part.
Look at the html output source :
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsFileTest</title>
<script type="text/javascript">
function myFunction()
{
alert("Hi from jsFile.php");
}
</script>
</head>
<body>
myID = idontknow<br>
<button onclick="myFunction()">Try it</button>
</body>
</html>
As you can see , javascript ( function myFuntion() ) is inserted directly in the html output. That is exactly what does not happen with
<title>jsFileTest</title>
<script type="text/javascript" src="jsFile.js"></script>
You can not use src="jsFile.php"
<script type="text/javascript" src="jsFile.php"></script>
After the parsing is finished the content is sent to the client. From this moment it is useless to even try to parse embedded php code in javascript. (the server is no longer involved , has already done its work)
IE detects an error (Status Line). when you double-click this
Error window pops up
the browser expects valid javascript code and this
$myId = "idontknow";
is not a valid JS code.
You just need to enable the PHP to read JS files. Do this:
Open your httpd.conf (Apache configuration file) and find this line:
AddHandler application/x-httpd-php .php
And add the extension, modifying this line to:
AddHandler application/x-httpd-php .php .js
You can even add CSS files too.
put this in customJs.php
<?php ob_start(); ?>
<script type="text/javascript">
<?php ob_end_clean(); ?>
alert('aaaa');
<?php ob_start(); ?>
</script>
<?php ob_end_clean(); ?>