- Home /
HTTP PUT in Unity for iOS?
Hello all,
I'm new here and I have a lot of fun developing with Unity, however I can't find a satisfying solution to my problem. I'm working on an iPhone app that has to send a HTTP PUT
request to a REST server at some point.
The first thing I did was to look at Unity's WWW
and WWWForm
classes, however they support GET
and POST
only.
So I decided to use .Net 2.0's HttpWebRequest
instead. I built my project with the full .Net 2.0 compatibility level (I used the subset until now), but I got the following error:
Uri putUri = new Uri("http://myserver.com/path");
HttpWebRequest request = WebRequest.Create(putUri) as HttpWebRequest;
That code works in Unity when I press play, but on the iPhone it throws:
MissingMethodException: Method not found: 'Default constructor not found...ctor() of System.Configuration.ExeConfigurationHost'. at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in :0 at System.Activator.CreateInstance (System.Type type) [0x00000] in :0 at System.Configuration.InternalConfigurationSystem.Init (System.Type typeConfigHost, System.Object[] hostInitParams) [0x00000] in :0 at System.Configuration.InternalConfigurationFactory.Create (System.Type typeConfigHost, System.Object[] hostInitConfigurationParams) [0x00000] in :0 at System.Configuration.ConfigurationManager.OpenExeConfigurationInternal (ConfigurationUserLevel userLevel, System.Reflection.Assembly calling_assembly, System.String exePath) [0x00000] in :0 at System.Configuration.ClientConfigurationSystem.get_Configuration () [0x00000] in :0 Rethrow as ConfigurationErrorsException: Error Initializing the configuration system. at System.Configuration.ClientConfigurationSystem.get_Configuration () [0x00000] in :0 at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection (System.String configKey) [0x00000] in :0 at System.Configuration.ConfigurationManager.GetSection (System.String sectionName) [0x00000] in :0 at System.Net.WebRequest..cctor () [0x00000] in :0 Rethrow as TypeInitializationException: An exception was thrown by the type initializer for System.Net.WebRequest at REST.MakePutRequest (System.String data) [0x00027] in /Users/Antho/Unity Project/Assets/Scripts/Network/REST.cs:68
I originally thought the problem originated in using a string in WebRequest.Create()
, but changing to an URI object didn't help. I tried to remove the HttpWebRequest
casting and use WebRequest objects instead, it didn't help.
My REST.cs
file contains other functions for GET
, POST
and DELETE
methods and they also work in Unity and break in the iPhone app.
The last ditch solution would be to call an external function that's hooked to the HTTP framework in Objective-C, but I don't want to do that before I make sure I cannot use HttpWebRequest
/WebRequest
in Unity for iOS.
Thanks!
Antho
Related questions:
http://answers.unity3d.com/questions/37162/webrequest-create-problem http://answers.unity3d.com/questions/55176/www-put-method
Feature request: http://feedback.unity3d.com/suggestions/support-put-in-www
Answer by chadcarter · Mar 16, 2015 at 10:06 PM
Assuming your server side webservice can support it, you can use the method override in an HTTP Header.
I'd like to see these HTTP verbs implemented as part of the WWW object, but in the mean time most server side technology can utilize the X-HTTP-Method-Override HTTP Header.
Make sure you pass the value through the X-HTTP-Method-Override header: headers["X-HTTP-Method-Override"] = "PUT"; (or PATCH, DELETE, GET, POST, etc)
byte[] postData = Encoding.UTF8.GetBytes(json);
Dictionary headers = new Dictionary(); headers.Add("Content-Type", "application/json"); headers.Add("Accept", "application/json"); headers["X-HTTP-Method-Override"] = "PUT"; www = new WWW("http://webservice-location/" + playerInfo.id.ToString(), postData, headers); yield return www;
I use this with Azure Mobile Services quite nicely.
Answer by rijkens · Jan 11, 2018 at 01:48 PM
Below the full example code can be found to do a PUT request in Unity:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using System.Collections.Generic;
public class PutRequest : MonoBehaviour
{
public string YourPostData;
public string APIUrl;
void Start()
{
//starts the put request example
StartCoroutine(PUTrequest());
}
IEnumerator PUTrequest()
{
// this string is the data that needs to be send to the server
byte[] postData = System.Text.Encoding.UTF8.GetBytes(YourPostData);
// The headers settings in order for the server to accept the PUT request
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
headers.Add("Accept", "application/json");
headers["X-HTTP-Method-Override"] = "PUT";
// the postData and header settings are send to the server
WWW api = new WWW(APIUrl, postData, headers);
yield return api;
// the response of the server can be read in the Debug Log
Debug.Log(api.text);
}
}
Your answer
Follow this Question
Related Questions
Get data from REST works on Unity but not on Android Device 1 Answer
IOS HTTP GET with Authorization Header not being sent 0 Answers
Why do I get Parse error (code 400) from Google Ftiness API in Unity? 0 Answers
HTTP WebRequest with iOS Stripping 1 Answer
Does overriding UnityWWWRequestDefaultProvider works for UnityWebRequest? 0 Answers