I am using UNITYHTP () to call a REITY API (KiiCloud) and it works great , But I am getting rid of it from third party libraries if possible and use it to use Unity's WWW and WWWForm.
Here's the code that uses Unity HTTP that works fine:
Public static zero runover extension (string) App ID, string apk, string endpoint, String kii_access_token, string msg) {hashtable data = new hashtable (); // Add json field with value here (use as dictionary) data. Add ("Message", msg); // When you pass a hashtable as a third argument, we believe that you want to send it as JSON-encoded // data. We will need to set the content-type header for you to encode JSON and application / Jason HTTP. Request myRequest = new HTTP.Request ("post", "https://api.kii.com/api/apps/" + + App ID + "/ server-code / version / current /" + end point, data); MyRequest.AddHeader ("x-kii-appid", appId); MyRequest.AddHeader ("x-kii-appkey", apk); If (kii_access_token! = Null) theRequest.AddHeader ("Authority", "Bearer" + kii_access_token); MyRequest.Send ((requested) = & gt; {// We offer object and array feature methods that try to parse the answer as JSON / if the response can not be parsed, then we can empty / Will come back / note that if you want to send Json which is not an object ({...}) or an array ([...]), it means that you use JSON.JsonDecode directly on the feedback Text, Object and Array // Only feature Hashtabl E result = request.response.Object; if (result == blank) {Debug.LogWarning ("JSON reply could not be parsed!"); Return;} Debug log ("Receive feedback"); Debug log ( Request .response.Text);}); }
So the above work is just fine but when I switch to the WWWForm like this:
public stable WWW RunServerExtension (string apid, string apke, String endpoint, string kii_access_token, string msg) {WWWForm form = new WWWForm (); Hashtable Header = Form. Header; Header ["content-type"] = "app / jason"; Header ["x-kii-appid"] = appId; Header ["x-kii-appkey"] = the app's; If (kii_access_token! = Null) header ["authorization"] = "bearer" + kii_access_token; Form.AddField ("Message", msg); Return to the new WWW ("https://api.kii.com/api/apps/" + appId + "/ server-code / versions / current /" + endpoint, form.data, headers); } Private internator WaitForRequest (WWW www) {yield returns www; // Check for errors if (www.error == faucet) {Debug.Log ("WWW is OK!:" + Www.text); } Else {Debug.log ("WWW error:" + www.error); }}
I get a bad request on the server (which means that the request is wrong, what the server was not expecting). Note that the header must be passed as a parameter otherwise the server does not complain about the missing header.
I suspect it may be related to the fact that the server expects JSON data, so I can use the Unity HTTP JSON class (You can use that separate class for JSON encoding / decoding This method passes {"message": "This is happening !!"} as the data:
Public stable WWW RunServerExtension (string appId, String apk, string endpoint, string kii_access_token, string msg) {WWWFormFor = New WWWForm (); Hashtable Header = Form. Header; Header ["content-type"] = "app / jason"; Header ["x-kii-appid"] = appId; Header ["x-kii-appkey"] = the app's key; If (kii_access_token! = Null) header ["authorization"] = "bearer" + kii_access_token; Hashtable data = new hashtable (); Data ["message"] = msg; Byte [] bytes = GetBytes (JSON.JsonEncode (data)); New WWW return ("https://api.kii.com/api/apps/" + appId + "/ server-code / version / current /" + end point, bytes, header); } Fixed byte [] GetBytes (string str) {byte [] bytes = new byte [str.Length * sizeof (char)]; System.Buffer.BlockCopy (str.ToCharArray (), 0, bytes, 0, bytes lang); Return bytes; }
But still the same bad request. Do you see why this may fail? Why does UnityHTTP work?
As I said in the comments:. If your webserver is expecting a different encoding, then simply passing by bytes will not literally yield good results.
, but if this API explicitly specifies the input / output encoding it is best.
I took a little longer, today. If you check the source of UnityHTP, you can see that:
this.bytes = encoding.UTF8.GetBytes (JSON.JsonEncode (data));
Your code does not change the string of the string, which means that you are sending the wrong bytes.
Comments
Post a Comment