I have a JSON is structured as:
{
"values": {
"AppName": "Test001",
"AppUser": "Testttt"
},
"consentAccepted": true,
"consentToken": "adgrtztzEZKo3LD56Hjo8LiqeoQ2Z5 ik0loplr"
}
The above JSON works in https://web.postman.co/ and I get status as success.
I would like to try Unity's WebRequest with POST to fill in "AppName" and "AppUser" along with token. How do I achieve this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class SendData: MonoBehaviour {
void Update () {
if (InputCompat.GetKeyDown (KeyCode.P)) {
StartCoroutine ("FillAndSend");
}
}
public IEnumerator FillAndSend() {
WWWForm form = new WWWForm ();
form.AddField ("AppName", "Testttt");
form.AddField ("AppUser", "Reinnn");
UnityWebRequest www = UnityWebRequest.Post ("https://mywebsite.com/profile/MyApp", form);
www.SetRequestHeader ("token", "tz677zuiuis2qEZKo3Lt kHluOGss");
yield return www.SendWebRequest ();
if (www.isNetworkError || www.isHttpError) {
Debug.Log (www.error);
} else {
Debug.Log (www.downloadHandler.text);
}
}
}
I receive an error named as HTTP internal server error. What is wrong with it? Along with this I have to add a certain parameter that is given as:
formToken: MyApp
CodePudding user response:
Looks like instead of a form
you rather want to send a JSON string.
Something like
var json = "{\"values\": {\"AppName\":\"Test001\",\"AppUser\":\"Rein\"}, \"consentAccepted\":true, \"consentToken\":\"tz677zuiuis2qEZKo3Lt kHluOGss\"}";
var jsonBytes = Encoding.UTF8.GetBytes(json);
using (var www = new UnityWebRequest("https://mywebsite.com/profile/MyApp", "POST"))
{
www.uploadHandler = new UploadHandlerRaw(jsonBytes);
www.downloadHandler = new DownloadHandlerBuffer();
www.SetRequestHeader("Content-Type", "application/json");
www.SetRequestHeader("Accept", " text/plain");
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log(www.downloadHandler.text);
}
}
CodePudding user response:
It seems more to me an Authorization
token problem. In order to try that, I created a request with these parameters as shown in the code, but I am getting a webpage in response it appears.
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class CustomRequest : MonoBehaviour {
#region Private Field
private string _url = "http://mywebsite.com/profile/MyApp";
private string _jsonString = "{\"values\": {\"AppName\": \"Test001\",\"AppUser\": \"Rein\"} }";
private string _token = "tz677zuiuis2qEZKo3Lt kHluOGss";
#endregion Private Field
#region MonoBehaviour Callback
private void Start () {
StartCoroutine ( SubmitRequest () );
}
#endregion MonoBehaviour Callback
#region Coroutine Method
private IEnumerator SubmitRequest () {
using ( UnityWebRequest request = UnityWebRequest.Post ( _url, _jsonString ) ) {
request.SetRequestHeader ( "Content-Type", "application/json" );
request.SetRequestHeader ( "Authorization", "Bearer " _token );
yield return request.SendWebRequest ();
if ( request.result == UnityWebRequest.Result.Success ) {
Debug.Log ( $"<color=lime>Response Date: {request.downloadHandler.text}</color>" );
}
}
}
#endregion Coroutine Method
}