Urban Airship posting data - urbanairship.com

How should i post data to urban airship so it works correctly? I've tried this, but get error that i havent posted any data
<form enctype="application/json" method="POST" action="https://go.urbanairship.com/api/push/broadcast/">
<input type="hidden" name="username" value="my_app_key"/>
<input type="hidden" name="password" value="my_master_secret"/>
<input type="hidden" name="data" value='{
"aps": {
"badge": 15,
"alert": "Hello from Urban Airship!",
"sound": "cat.caf"
},
"exclude_tokens": [
]
}'/>
<button type="submit"/>
</form>

Browsers generally only support enctype "application/x-www-form-urlencoded" and "multipart/form-data". What's probably happening here is the form is not posting valid JSON, but instead url-encoded data where on of the keys is 'data' and the value is the long string of JSON.
Your best bet would be to call a javascript function when you press the submit button which posts the data with proper content-type and formatting. Using something like JQuery.post() http://api.jquery.com/jQuery.post/ would be pretty helpful.

Related

Correct way of post an object array using form post

I'm trying to use an payment service API in my application and following the documentation provided on the website. According to the docs, the POST body should look like this:
api_hash=38be425a-63d0-4c46-8733-3e9ff662d62d
&hash=ac0945d82b8589959b5f4ffafcc1a6c5983e82b8b4094c377a7b9c43d4a432bc
&order_id=2845
&amount=15
&currency=EUR
&email=stefan#my-test-store.com
&url_failure=http://my-test-store.com/order/fail
&url_ok=http://my-test-store.com/order/success
&items=[{"sku":"450","name":"Test Item","amount":"15","type":"item_type","qty":"1","price":15,"id":"5619","url":"http://example.com/products/item/example-item-name-5619"}]
I was able to successfully make a post request with postman however I'm confused about the "items" part in the post body since it's an object array. My html form looks like this:
<form method="post" action="#ViewBag.G2AConfig.PostUrl" id="g2apaymentform">
<!--PayPal Settings-->
<input type="hidden" name="api_hash" value="#ViewBag.G2AConfig.ApiHash" />
<input type="hidden" name="hash" value="#ViewBag.Hash" />
<input type="hidden" name="order_id" value="#ViewBag.OrderId" />
<input type="hidden" name="amount" value="#Model.Total" />
<input type="hidden" name="currency" value="USD" />
<input type="hidden" name="email" value="#ViewBag.G2AConfig.MerchantEmail" />
<input type="hidden" name="url_failure" value="#ViewBag.UrlFailure" />
<input type="hidden" name="url_ok" value="#ViewBag.G2AConfig.ReturnUrl" />
#foreach (var item in Model.Items)
{
<input type="hidden" name="items[#index][sku]" value="#item.Product.GameAccountId" />
<input type="hidden" name="items[#index][name]" value="#item.Product.Rank.Name" />
<input type="hidden" name="items[#index][amount]" value="#item.Product.MarketPrice" />
<input type="hidden" name="items[#index][qty]" value="1" />
index = index + 1;
}
</form>
I'm using Ajax post to make the request which looks like this:
event.preventDefault(); //prevent default action
var post_url = $(this).attr("action"); //get form action url
var request_method = $(this).attr("method"); //get form GET/POST method
var form_data = $(this).serialize(); //Encode form elements for submission
$.ajax({
url: post_url,
type: request_method,
data: form_data
}).done(function (response) { //
$("#server-results").html(response);
});
This does not work and I get bad error response from the server. What's the correct way to submit a form with an object array?
Why do you create a form with the supposed objects before posting it?
If there is no real reason other than posting the values then i suggest you build the AJAX data via a javascript array or PlainObject as these are the other 2 possible data types for the data parameter. Currently you are using the serialized string.

validate input text with angularJS

I've got a form which adds items in my todo list array.
However if the input field is empty, my function is adding an empty value to my array.
What is the best way to validate the form?
<form name="formaddtodo" ng-submit="todoList.addTodo()">
<input type="text" ng-model="todoList.todoText" ng-minlength="1" size="30"
placeholder="Voeg nieuwe todo toe">
<input class="btn-primary" type="submit" value="voeg toe">
</form>
Here is my function
todoList.addTodo = function() {
todoList.todos.push({text:todoList.todoText, done:false});
todoList.todoText = '';
};
There is no validation yet but I'm curious what the best way is to validate.
Thank you in advance!
You can use the built-in ngRequired directive:
<input type="text" ng-model="todoList.todoText" ng-minlength="1" size="30"
placeholder="Voeg nieuwe todo toe" ng-required="required">
AngularJS provides basic implementation for most common HTML5 input
types: (text, number, url, email, date, radio, checkbox), as well as
some directives for validation (required, pattern, minlength,
maxlength, min, max).
https://docs.angularjs.org/guide/forms
<form name="formaddtodo" ng-submit="todoList.addTodo(formaddtodo.$valid)">
<input type="text" ng-model="todoList.todoText" ng-minlength="1" size="30"
placeholder="Voeg nieuwe todo toe" required>
<input class="btn-primary" type="submit" value="voeg toe">
</form>
todoList.addTodo = function(isValid) {
if(isValid) {
todoList.todos.push({text:todoList.todoText, done:false});
todoList.todoText = '';
}
};
I think the above code will help you to validate your form.
ngMessages module support for displaying messages to the user for form validation.
ngMessages, developers were forced to rely on directives such as ng-class and ng-show to display these errors.
Please follow this link. It might be useful for you now and in future use for validation with AngulerJS.

Passing content to a URL (Who.is)

I am going to need to add a form to a page on a site, that takes a domain name as the input from the viewer and sends it to who.is so that the DNS for this domains a returned.
When searching onwho.is, the URL for the search query when looking for me.com looks like this:
http://who.is/dns/me.com
I am struggling here since I can't pass the domain name using the variable form, with ?q=...
Any Ideas?
This is so far the html I have for this form:
<form method="get" action="http://who.is/dns/" target="_blank">
<input type="text" name="q" size="31" maxlength="255" placeholder="Who is" width="255"/>
</form>
Thanks
You will not be able to do this in HTML unless you find a URL with parameters, like TechnoKnol posted.
You need this or a server process
window.onload=function() {
document.getElementById("who").onsubmit=function() {
window.open("http://who.is/dns/"+document.getElementById("q").value(),"_blank");
return false;
}
}
using
<form id="who">
<input type="text" id="q" size="31" maxlength="255" placeholder="Who is" width="255"/>
</form>
By inspecting on who.is, I found below url
http://who.is/search.php?search_type=Whois&query=me.com&commit=Search
With some modification in your form will work.

multiple submit button values with multipart/form-data

Working on a Multipart File Upload form. Using Java with Servlet 3.0. It seems with multipart, the value of the submit button is not passed to the server?
e.g.
<form method="POST" enctype="multipart/form-data" action="/servlet">
<input type="hidden" name="mode" value="image">
<input type="hidden" name="id" value="123">
<input type="file" name="file" id="file">
<input type="submit" name="action" value="Upload">
<input type="submit" name="action" value="Delete">
</form>
In a regular post, you would just check the value of the "action" parameter. How do you access this in a multipart form? I tried examining the Parts in the request, and it's just not there.
e.g.
Collection<Part> parts = request.getParts();
System.out.println("parts: "+parts.size());
for(Part part : parts){
System.out.println(part.getName());
}
Outputs:
parts: 3
mode
id
file
As in...
String mode = request.getParameter("mode"); //reads "image"
String id = request.getParameter("id"); //read "123"
String action = request.getParameter("action"); //reads null
How do you solve this one?
Have a hidden field declared and set its value based on the Submit button clicked , before you submit , using Javascript
<input type="hidden" name="submitType" value="">
first import multipart jar file in your program then
try to use multipartrequest class for accessing multipart data from your form -
MultipartRequest mpr=new MultipartRequest(request,"C:");
String ad=mpr.getParameter("made"); // read "image"
String ad=mpr.getParameter("id"); // read "123"
String ad=mpr.getParameter("action"); // if u submit from from upload then it read "upload" or submit from delete then it read "delete" String .

Google Drive API Authentication

I'd like to make an application that could access MY own Google Drive anytime, create files there, share them and so on.
According to https://developers.google.com/drive/service-accounts "Use regular Google accounts as application-owned accounts" the only think I need is to get access_token and refresh_token once, store them in my application and using refresh_token I can refresh my access_token (somehow).
I can get access_token using request something like
https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/drive.file&redirect_uri=http://localhost;response_type=token&client_id=
After approve this application request in user dialog I will be redirected to my localhost and I will get access_token that expires in 3600 seconds.
The questions are:
1. How to get a refresh_token?
2. How to refresh access_token using refresh_token?
I don't want to use Google's API client library because it's terrible (.NET).
Ok I got it.
The answer can be found here: https://developers.google.com/accounts/docs/OAuth2WebServer#offline
First You have to make an Auth request
<form method="POST" action="https://accounts.google.com/o/oauth2/auth">
<input type="hidden" name="scope" value="[YOUR SCOPE]"/>
<input type="hidden" name="client_id" value="[YOUR CLIENT ID]"/>
<input type="hidden" name="response_type" value="code"/>
<input type="hidden" name="redirect_uri" value="[YOUR RETURN URL]"/>
<input type="hidden" name="access_type" value="offline"/>
<input type="submit"/>
</form>
Then you will get a 'code' to your return_url
Then you need to exchange the code to access_token and refresh_token
<form method="POST" action="https://accounts.google.com/o/oauth2/token">
<input type="text" name="code" value="[CODE YOU GOT IN PREV STEP]"/>
<input type="hidden" name="client_id" value="[YOUR CLIENT ID]"/>
<input type="hidden" name="client_secret" value="YOUR CLIENT SECRET"/>
<input type="hidden" name="grant_type" value="authorization_code"/>
<input type="hidden" name="redirect_uri" value="YOUR REDIRECT URL"/>
<input type="submit"/>
</form>
As a result of this you will bet response like:
{
"access_token" : "[HERE YOU ACCESS TOKEN]",
"token_type" : "Bearer",
"expires_in" : 3600,
"id_token" : "eyJhbGciOiJSUzI1NiIsImtpZCI6ImRiMjBlNWMwZGU1YWI0MGRjNTU5ODBkM2EzYmZlNDdlOGM2NGM5YjAifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiY2lkIjoiMjQ2ODg5NjU3NDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXVkIjoiMjQ2ODg5NjU3NDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwidG9rZW5faGFzaCI6IjRURGtlQ0MzVWRPZHoyd2k1N2RnaUEiLCJpZCI6IjExNTI0MDk1NDM0Njg1NTU4NjE2MSIsImlhdCI6MTM1MzQwNDQ3MCwiZXhwIjoxMzUzNDA4MzcwfQ.Va98sh9LvMEIWxpRMFkcuFqtDAUfJLN5M__oJyjvmIxQR9q2NUIoocyjqbNyXc7as_ePQYiUjajx0SCumtR4Zhv-exeJfrKA_uMmJTe7jWhK6K2R3JQ2-aIZNnehpEuhYZBXgLhzYz1mlFrLqQTdV6LjDhRPDH-ol4UKWXfbAVE",
"refresh_token" : "[HERE YOUR REFRESH TOKEN]"
}
Now you can store these tokens in your application and use for unlimited time refreshing the access_token every 3600 secs
<form method="POST" action="https://accounts.google.com/o/oauth2/token">
<input type="text" name="refresh_token" value="[YOUR REFRESH TOKEN]"/>
<input type="hidden" name="client_id" value="[YOUR CLIENT ID]"/>
<input type="hidden" name="client_secret" value="[YOUR CLIENT SECRET]"/>
<input type="hidden" name="grant_type" value="refresh_token"/>
<input type="submit"/>
</form>
And each time you make this request you will get a new access_token
{
"access_token" : "[NEW ACCESS TOKEN]",
"token_type" : "Bearer",
"expires_in" : 3600,
"id_token" : "eyJhbGciOiJSUzI1NiIsImtpZCI6ImRiMjBlNWMwZGU1YWI0MGRjNTU5ODBkM2EzYmZlNDdlOGM2NGM5YjAifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiYXVkIjoiMjQ2ODg5NjU3NDg2LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwidG9rZW5faGFzaCI6ImpyYk5oNkRHZFN4Y0w5MUI5Q1hab2ciLCJpZCI6IjExNTI0MDk1NDM0Njg1NTU4NjE2MSIsImNpZCI6IjI0Njg4OTY1NzQ4Ni5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImlhdCI6MTM1MzQwNTU5OSwiZXhwIjoxMzUzNDA5NDk5fQ.mGN3EYOX75gPubr3TqWIOBkfq-o3JBXMXx4MbxEBGMSuPdJi7VTqZa4isyR-st-J5_wTtA-j8tVQYnDeZDxj5KpJ14FFQPKTtv_VI5kvuT55KyOmGu4yidciYoffJMISisr8NqiksbemaiYX900sRv6PmoTA6Nf6VtHgj3BZjWo"
}