Home > Software engineering >  Sending a post request with body and a header in it with UnityWebRequest
Sending a post request with body and a header in it with UnityWebRequest

Time:10-01

I am trying to send a post request to an Api called Paycell Api. I dont need to authenticate for testing the request. When I send the request with PostMan, it returns 200 OK. Here is the exact request.

{
   "msisdn": "5380521479",
   "requestHeader":    {
      "applicationName": "PAYCELLTEST",
      "applicationPwd": "PaycellTestPassword",
      "clientIPAddress": "10.252.187.81",
      "transactionDateTime": "20160309084056197",
      "transactionId": "12345678901234567893"
   }
}     

When ı try to implement it to C# it returns 406 Not Acceptable. Here is how it looks

using Newtonsoft.Json;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
public class GetCardsRequest : MonoBehaviour
{
    private string url = "https://tpay-test.turkcell.com.tr:443/tpay/provision/services/restful/getCardToken/getCards/";


    public void GetCards()
    {
        StartCoroutine(MakeCardRequest());
    }

    IEnumerator MakeCardRequest()
    {

        var bodyRequest = new GetCardRequest() {

            requestHeader = new RequestHeader()
            {
                applicationName = "PORTALTEST",
                applicationPwd = "ZDyXmMLWE2z7OzJU",
                clientIPAddress = "10.252.187.81",
                transactionDateTime = "20160309084056197",
                transactionId = "12345678901234567893"
            },

            msisdn = "5380521479"
        };

        bodyRequest.requestHeader = new RequestHeader();

        var body = JsonConvert.SerializeObject(bodyRequest);

        UnityWebRequest request = UnityWebRequest.Post(url, body);
        request.SetRequestHeader("Content-Type", "application/json");
        request.SetRequestHeader("Accept", "text/csv");


        yield return request.SendWebRequest();

        if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
        {
            Debug.Log(request.error);
        }
        else
        {
            //var cardName = JsonConvert.DeserializeObject<GetCardResponse>(request.downloadHandler.text);

        }

    }

}

İf I delete the part

request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Accept", "text/csv");

It returns 415 Unsupported Media Type Error.

How can I send this request, please help me.

Here's the documentation of the API, it shows an example request. Paycell API

And here is how I implemented GetCardRequest

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class RequestHeader
{
    public string applicationName { get; set; }
    public string applicationPwd { get; set; }
    public string clientIPAddress { get; set; }
    public string transactionDateTime { get; set; }
    public string transactionId { get; set; }
}

public class GetCardRequest
{
    public string msisdn { get; set; }
    
    public RequestHeader requestHeader { get; set; }
}

CodePudding user response:

Now with your code the first main issue is mot probably that your JSON is always empty fields.

you are doing

bodyRequest.requestHeader = new RequestHeader();

which erases all the previously filled in values


and then as mentioned before another thing to try might be rather using

IEnumerator MakeCardRequest()
{
    var bodyRequest = new GetCardRequest()
    {
        requestHeader = new RequestHeader()
        {
            applicationName = "PORTALTEST",
            applicationPwd = "ZDyXmMLWE2z7OzJU",
            clientIPAddress = "10.252.187.81",
            transactionDateTime = "20160309084056197",
            transactionId = "12345678901234567893"
        },

        msisdn = "5380521479"
    };

    var body = JsonConvert.SerializeObject(bodyRequest);
    var bytes = Encoding.UTF8.GetBytes(body);

    using(var request = new UnityWebRequest(url, "POST"))
    {
        request.uploadHandler = (UploadHandler) new UploadHandlerRaw(data);
        request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
        request.SetRequestHeader("Content-Type", "application/json");

        yield return request.SendWebRequest();

        if (request.result != UnityWebRequest.Result.Success)
        {
            Debug.Log(request.error);
        }
        else
        {
            var cardName = JsonConvert.DeserializeObject<GetCardResponse>(request.downloadHandler.text);
            Debug.Log(cardName);
        }
    }
}
  • Related