- Home /
Including POST request data for RESTfull server
I am still a little unclear on how to handle POST request data especially bound for a RESTfull server for the purpose of quarrying a database. currently we have a noSQL database in place that expects curl-i notation. (I don't know how this directly works was just given list of "acceptable" commands)
currently all I know how to do is actions that require just a url (getting X number of records at known location [Yrecords starting at positionX]). but what I need to implement is searching which requires a special formatted POST request, and the documentation is really no help at all. the expected statement in curl-i is:
curl -i "urlTarget" -XPOST -d'{keyValue:"targetValue"}'
so what/how do I give this to the server using the WWW class call? do I need to wrap this -d'{keyValue:"targetValue"}'
in a WWWForm, hand it using a byte[]
, or do more rigorous string magic on it?
is there anywhere I can look specifically for more in depth information on the WWWForm besides here, or even more in depth information of the different WWW class constructors?
even a tutorial, or a functional demonstration program
I found something similar for the formatting here, but when I try this feeding in "keyValue" as formArg1, and "targetValue" as formArg2 the server tells me that these are bad requests even if I take the search data directly from a record I just pulled.
anyone?
additional information available in forums here help in either of these places would mean success of project as this is a viability project at its core.
Answer by gardian06 · Mar 05, 2013 at 08:37 PM
if post request data must be given in the immediate header as with the question then it should be placed in a hashtable, and then encoded into a WWW call as a header like so:
var encoding = new System.Text.UTF8Encoding();
var postHeader = new Hashtable();
postHeader.Add("Content-Type", "text/json");
postHeader.Add("Content-Length", jsonString.Length);
print("jsonString: " + jsonString);
var request = WWW(postScoreURL, encoding.GetBytes(jsonString), postHeader);
yield request;
though keep in mind that even though the encoding reads as json the formatting might not be strict json, but a modification thereof.
in regards to doing this through WWWForm this requires either talking to a specific script on the server, or talking to the only script on the given server, and is detailed in my answer here
Answer by tzamora · May 07, 2013 at 04:19 PM
In my case to connect to .net web api (rest) I did this:
WWWForm form = new WWWForm();
form.AddField("facebookID", facebookID);
//
// I dont know why, but I have to put the parameters repeated in the url and in the WWWForms
// If I put the post argument only in the WWWForm or only in the url the request returns 404
//
string url = "http://site.net/api/facebookusers/?facebookID=" + facebookID;
Hashtable header = new Hashtable();
header.Add("Content-Type", "application/x-www-form-urlencoded");
WWW www = new WWW(url, form.data, header);
StartCoroutine( WaitForRequest( www ) );
Answer by nventimiglia · Feb 22, 2013 at 05:22 PM
I just finished writing a ASP.NET MVC4 API service to talk with Unity3d. My controller logic talks to a live database (via entity), but that shouldn't matter.
Unity side my logic looks something like this.
var form = new WWWForm();
form.AddField("key", key);
form.AddField("content", content);
//Secure method
var www = new WWW(Domain+SaveUrl, form.data, AuthCookie.SessionCookie);
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
callback.Invoke(new ApiResult
{
HasError = true,
Message = www.error
});
}
else
{
callback.Invoke(new ApiResult());
}
The callback is a action delegate for communicating results back to consumers of this service client.
in your 2 lines including form.AddField()
what to do those correlate too? this is the part I think I am getting wrong, but the responses of the server is only the 1 if it is incorrect.
Those are the parameters i am sending to my server side action.
ie : Login : username/value password/value
Can you post the $$anonymous$$VC side controller code for this example? Like you I have a running $$anonymous$$VC 4 api tied to a database tied to Unity3d. I have no issues sending files, and other data to unity3d. But when I try to post data from unity3d to the $$anonymous$$VC API the form data is not bound to the control function. ie. the form data always come in as null. I have found a number of web postings on $$anonymous$$VC 4 and difficulties with API binding.
$$anonymous$$any Thanks
Answer by gaminggal39 · Nov 12, 2018 at 07:50 AM
Watch this video to solve :https://www.youtube.com/watch?v=FChVgb6EXGk&t=133s
Your answer
![](https://koobas.hobune.stream/wayback/20220613093047im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to Upload multiple files to a server using UnityWebRequest.Post(); 3 Answers
Getting info using WWW & PHP 3 Answers
How do I make a simple POST request to Amazon S3? 2 Answers
WWW/WWWForm, does Unity validate SSL certificates? 1 Answer
Listing packages from web directory and loading them during runtime 0 Answers