- Home /
UnityWebRequest.Post with MultipartFormDataSection returns Bad Request (400)
Hello,
I've created a node.js server to post data to database table, this is my node.js code snippet:
router.post('/register', async function(req,res){
try{
const {id, name} = req.query;
const sqlQuery = 'INSERT INTO TestTable (id, name) VALUES (?,?)';
const result = await pool.query(sqlQuery, [id, name]);
console.log(result);
res.status(200).json(result);
} catch(error){
res.status(400).send(error.message)
}
})
Following a tutorial (https://www.youtube.com/watch?v=krTbf0O-BCo), I first tested the post method with Postman software, and I was able to write to the TestTable with the localhost succesfully:
Next step was to call the "post" method using REST API from Unity, and my goal was to pass the URL, similarly to the URL as it is in postman:
localhost:8000/user/register?id=2232&name=somename
This is the "Post" method code snippet from Unity:
private const string POST_DB_URL = "http://localhost:8000/user/register";
private IEnumerator PostCourseCreditInternal() {
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add(new MultipartFormDataSection("id=5555&name=somename"));
var postRequest = UnityWebRequest.Post(POST_DB_URL, formData);
yield return postRequest.SendWebRequest();
if (postRequest.isHttpError) {
Debug.Log("request resulted in error: " + postRequest.error);
}
if (postRequest.downloadHandler.isDone) {
Debug.Log(postRequest.downloadProgress);
}
}
This returns an error 400 - bad request, I wonder if it is the syntax or maybe some privacy limitations due to VPN usage in my work?
Should I use the UnityWebRequest.Post
or should I use UnityWebRequest.Get
to post?
Thanks for any help
Your answer
Follow this Question
Related Questions
HELP! 400 Bad Request on me/invitable_friends 2 Answers
PlayFab: Getting player title data with leaderboard? 0 Answers
How to add post from wordpress in unity? 0 Answers
I cant get response json from rest call in Unity 1 Answer
Reading JSON response from Google Sheets in unity ( most places have WWW.text) 1 Answer