I’m working on the client code for my RESTfull service, and wondering is there any way to send POST request using UnityWebRequest.Post with json data in body instead of WWWForm?
But server returns a 400 error, meaning that request was not correct.
By the way, maybe some one also know a way to debug HTTP calls of UnityWebRequest? I tries to catch them with Fiddler but without any success.
Based on the documentation, I’m not sure why your code compiles. Looks like there are these options when building a WWW object:
public WWW(string url);
public WWW(string url, WWWForm form);
public WWW(string url, byte[ ] postData);
public WWW(string url, byte[ ] postData, Hashtable headers);
public WWW(string url, byte[ ] postData, Dictionary<string,string> headers);
However, you seem to be using a non-existent
public WWW(string url, string content);
constructor. My only guess is that, either your code doesn’t compile, or WWWForm has an implicit conversion from string, which probably doesn’t produce what you expect.
Having never done this before, my suggestion is to convert your string into a byte[ ] before passing to the WWW class. You can do that with one of the Encodings, though you probably just want UTF8.
For anyone coming here in 2019 using Unity 2018 (I’m on 2018.3.10f1), this answer is no longer correct since Unity has changed the API for UnityWebRequest.Post().
The way i solved it was not using the .Post() method, but instead configure it all myself. The reason why i need to do so, is that Unity automatically URL encodes your request body string, without you having an easy way to disable that, see the documentation:
//
// Summary:
// Creates a UnityWebRequest configured to send form data to a server via HTTP POST.
//
// Parameters:
// uri:
// The target URI to which form data will be transmitted.
//
// postData:
// Form body data. Will be URLEncoded prior to transmission.
//
// Returns:
// A UnityWebRequest configured to send form data to uri via POST.
public static UnityWebRequest Post(string uri, string postData);
Heres my solution:
My Solution
public UnityWebRequest CreateApiGetRequest(string actionUrl, object body = null)
{
return CreateApiRequest(BaseUrl + actionUrl, UnityWebRequest.kHttpVerbGET, body);
}
public UnityWebRequest CreateApiPostRequest(string actionUrl, object body = null)
{
return CreateApiRequest(BaseUrl + actionUrl, UnityWebRequest.kHttpVerbPOST, body);
}
UnityWebRequest CreateApiRequest(string url, string method, object body)
{
string bodyString = null;
if (body is string)
{
bodyString = (string)body;
}
else if (body != null)
{
bodyString = JsonUtility.ToJson(body);
}
var request = new UnityWebRequest();
request.url = url;
request.method = method;
request.downloadHandler = new DownloadHandlerBuffer();
request.uploadHandler = new UploadHandlerRaw(string.IsNullOrEmpty(bodyString) ? null : Encoding.UTF8.GetBytes(bodyString));
request.SetRequestHeader("Accept", "application/json");
request.SetRequestHeader("Content-Type", "application/json");
request.timeout = 60;
return request;
}
Usage:
Usage
var request = webApi.CreateApiGetRequest("/test"); // Get request with blank request body
[Serializable]
public class ApiAuthenticateRequest
{
public string Email;
public string Password;
}
var request = webApi.CreateApiPostRequest("/auth", new ApiAuthenticateRequest { Email = "email@example.com", Password = "correcthorsebatterystaple" } ); // Post request with JSON request body