That’s excellent. Sounds like you need charles or wireshark or some kind of proxy to figure out how the two requests differ, and then to adjust so they comply.
Right, HTTP response code 5xx indicates a server side issue. Of course it’s possible that the client may send the wrong data so the server can’t handle the request properly. However in any case, without knowing what the server expects or what may go wrong on the server side we can only guess. What we can tell is that the server is having an issue.
Apart from that we can also tell that this line makes no sense:
You’re sending an html form to your server. So it contains url encoded fields as well as binary data. As such the content type of the request must be “multipart/form-data” and Unity does handle this for you. So messing with the content type header makes no sense. This header also contains the boundary string that is required to properly decode the multipart sections. So you should remove that line.
If you want to specify the content type of your file upload, you have to pass the mime type to the “AddBinaryData” call. It has several overloads and there’s one that takes an additional file name and mime type.
Thank you sir for your reply
Hello sir, if you remember 3 to 4 days ago, you have helped me to send JSON string to web server and at that time, I was getting the same error code, 500 internal server error.
So at that time, we changed in the code little bit and its started working and in this case also within Postman it was working. https://discussions.unity.com/t/895522/5
This time, I want to send wave file data at web server through programmer web service, as like I have shown in the recorded video.
As per my thinking, I was sending wrong data to web server so it can’t able to process my web service request successfully.
After your above suggestion, I have changed my code something like this:
public void GetScoreByAudio(string filePath)
{
byte[] fileContent = Recorder.Recorder.ConvertWAVtoByteArray(filePath);
StartCoroutine(GetScoreByAudioEnumerator(fileContent));
IEnumerator GetScoreByAudioEnumerator(byte[] fileContent)
{
WWWForm form = new WWWForm();
form.AddField(TAG_AUDIO_WORD, "right");
form.AddField(TAG_AUDIO_FILE_NAME, "recordertest.wav");
form.AddBinaryData(TAG_AUDIO_FILE_DATA, fileContent, "recordertest.wav", "audio/vnd.wave");
//form.AddBinaryData(TAG_AUDIO_FILE_DATA, fileContent);
using (UnityWebRequest webRequest = UnityWebRequest.Post("https://gfbszns7ll.execute-api.eu-west-2.amazonaws.com/default/scoring-api-wrapper", form))
{
webRequest.SetRequestHeader(HEADER_AUTHORIZATION, DataStorage.RetrieveUserAccessToken());
//webRequest.SetRequestHeader("Content-Type", "audio/vnd.wave");
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
switch (webRequest.result)
{
case UnityWebRequest.Result.ConnectionError:
case UnityWebRequest.Result.DataProcessingError:
Debug.LogError("Error: " + webRequest.error);
break;
case UnityWebRequest.Result.ProtocolError:
Debug.LogError("HTTP Error: " + webRequest.error);
break;
case UnityWebRequest.Result.Success:
Debug.Log("Received: " + webRequest.downloadHandler.text);
break;
}
}
}
But unluckily the result was same.
Let me know PUT method, I can use or not.
This kind of structure, I got from the web developer:
This description doesn’t talk about sending a html form to the server but using get request parameters in the url itself. In that case you have to remove your whole “form” stuff and use something like this instead:
This is a normal upload without an html form. So we directly upload the audio data and the parameters are part of the url. Keep in mind that the currently hardcoded values “right” and “recordertest.wav” should be url encoded. If they are constant and do not change it should work that way. However if those should be provided externally those values should be put through UnityWebRequest.EscapeURL.
ps: If the “query parameters” confused you, have a look at the syntax of an URL.
One last question, two parameters I am sending with the web request, “file name” and “word”.
From these, “file name” I will not change but “word” value I have to change rather than static “right” word.
If I just place string variable over their rather than static “right” word then will it work?
Parameters in an URL must not contain any characters that have a special meaning inside ah URL. That includes things like /, &, %, :, ...and a few others. If the parameter value need to include any such special character, those characters need to be URL encoded. This is also true for the space character. A space has to be escaped to either %20 (which is hexadecimal for 32 which is a space in ASCII) or a + character.
As I said, if you know the values you’re going to use are safe to be used directly, there’s no need to escape them. However if the values can change or come from user input, you should always escape them.
@Bunny83 I don’t know why but again I started getting issue of:
HTTP Error: HTTP/1.1 500 Internal Server Error
Before it worked and now its completely stopped working, I didn’t made any changes after this.
I am attached the whole script so you can understand better way.