- Home /
Translate cURL -d to Unity C# is throwing error 400 Bad Request
Hi,
There's a web page where I want to make RESTful calls. However I am getting error 400 Bad Request. Testing from web page works just fine, however this error occurs only in unity.
cURL:
curl -X POST "http://MyWebSite.net/api/User/Authorize" -H "accept: text/plain" -H "Content-Type: application/json" -d "{\"userId\":\"i8PSadEvWAeOPFASfegE7sEPQPQO2\"}"
And this is how I am translating this cURL command to unity C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class GameTest : MonoBehaviour
{
//I changed following url with some non-existent link, just for security reasons.
public string url = "http://MyWebSite.net/api/User/Authorize";
// Start is called before the first frame update
void Start()
{
StartCoroutine(PostRequest());
}
IEnumerator PostRequest()
{
WWWForm form = new WWWForm();
//Uncommenting following line makes no difference
//form.AddField("userId", "i8PSadEvWAeOPFASfegE7sEPQPQO2");
using (UnityWebRequest www = UnityWebRequest.Post(url, form))
{
www.SetRequestHeader("accept", "text/plain");
www.SetRequestHeader("Content-Type", "application/json");
//Uncommenting following line makes no difference
//www.SetRequestHeader("userId", "i8PSadEvWAeOPFASfegE7sEPQPQO2");
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError("ERROR: " + www.error);
}
else
{
Debug.Log("Success");
}
}
}
}
I believe that I am not sending -d "{\"userId\":\"i8PSadEvWAeOPFASfegE7sEPQPQO2\"}" correctly from unity. Any help is appreciated.
Thanks
Odds are the bad request response contains more information about why the request was bad. What's in the body of the response?
At a guess - you're telling it that you're sending it json, but the WWWForm object is sending it form data, so it can't parse the request body as it's expecting json.
Hey @CorruptedTNC , I was trying to solve this issue myself, however I couldn't. This is response body. Any idea why is this happening?
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|247a7c9f-472dd4fa33a76c09.","errors":{"$":["'u' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0."]}}
Answer by K97 · Sep 10, 2020 at 07:48 AM
Fixed it :) For anyone having same issue, set www.uploadHandler to new object of type UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(JsonObject));
(credits)
, and make sure to not send data with form, but send json instead.
Complete script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class GameTest : MonoBehaviour
{
public string url = "http://MyWebsite.azurewebsites.net/api/User/Authorize";
// Start is called before the first frame update
void Start()
{
StartCoroutine(PostRequest());
}
IEnumerator PostRequest()
{
UserId user = new UserId
{
userId = "i234KfpsoAeOP2ODasdg532EPQPQO2"
};
string jsonText = JsonUtility.ToJson(user);
using (UnityWebRequest www = UnityWebRequest.Post(url, jsonText))
{
www.SetRequestHeader("accept", "application/json");
www.SetRequestHeader("Content-Type", "application/json");
www.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(jsonText));
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError("ERROR title: " + www.error);
Debug.LogError("ERROR body: " + www.downloadHandler.text);
}
else
{
Debug.Log("Success");
Debug.Log("Success body: " + www.downloadHandler.text);
}
}
}
}
Your answer
Follow this Question
Related Questions
UnityWebRequest ContentType not Overriding 4 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Unity iOS WWW POST failing ssl handshake 2 Answers