Post a Json

Forum rules
You must provide your invoice number to receive technical support. PM your invoice number to 'chris'.

Post a Json

Postby MrButterFingers » Wed Dec 06, 2017 2:36 am

Hello,

I have this JSON text:
Code: Select all

   "login":{ 
      "username":"userName",
      "password":"password",
      "DeviceSerialNum":"123456789"
   },
   "DeviceModuleID":"8",
   "version":"6.0",
   "isFixedLayout":"false",
   "applicationType":"3",
   "excludeIDs":[]
}


and I don't understand how to post it in the operation.

Thanks
MrButterFingers
 
Posts: 3
Joined: Wed Dec 06, 2017 2:27 am

Re: Post a Json

Postby chris » Wed Dec 06, 2017 5:53 pm

Hello Butters:

Please take a look at some of the documentation here: http://unity3dassets.com/category/wak/.

There are step by step instructions on how to convert your JSON request into a model and create a class around it.

Thanks
Chris
User avatar
chris
Site Admin
 
Posts: 97
Joined: Thu Feb 28, 2013 1:40 am
Location: Chicago, IL

Re: Post a Json

Postby MrButterFingers » Mon Dec 11, 2017 9:14 am

Hi Chris,

I Have searched the documantation, and I didnt find related Example.

I have created an oprtation:
Code: Select all
 [HttpPOST]
    [HttpPath(null,"{$ImageUri}")]
    [HttpContentType("application/json")]
    [HttpAccept("application/json")]
    [HttpTimeout(10f)]
    public class Login : HttpOperation
    {
        [HttpUriSegment]
        public string Uri;

        [HttpResponseJsonBody()]
        public string Respnse;

        [HttpRequestJsonBody()]
        public string JsonResqust;
}


And I'm using it like this:
Code: Select all
 void Start()
        {
            new EvritOperations.Login() {
                    Uri = "/LoginUser",
                    JsonResqust =  EvritJsonBodyRequest.Login(
                        _loginView.GetUserName(),
                        _loginView.GetPassword(),
                        Library.UDID)
                }
                .Send();
        }


and I need to send this Json in the request body:
Code: Select all
{
  "login": {
    "username": "butters@gmail.com",
    "password": "12345",
    "DeviceSerialNum": "TEST13-635B-4CE3-8A0B-C13A65DD85F0N",
    "DeviceModel": "None",
    "DeviceBrand": "None"
  },
  "DeviceModuleID": "8",
  "applicationType": "EvritForChildren",
  "deviceNickname": "deviceNickTest",
  "version": "6.0"
}


and I'm getting this Error:
<color=red>{ERR}</color> (SerializeLitJson)(Convert) Failure on field 'JsonResqust' : Can't add a value here

Why SerializeLitJson cant handle the json ?
is it something that I'm doing wrong ?
can you pls help me

Best regards
MrButterFingers
 
Posts: 3
Joined: Wed Dec 06, 2017 2:27 am

Re: Post a Json

Postby chris » Wed Dec 13, 2017 8:12 pm

I guess you're right, there isn't really a straight example.
Ok so from your JSON request, we have to create a class. I used this tool: http://json2csharp.com/#

Code: Select all
public class Login
{
    public string username;
    public string password;
    public string DeviceSerialNum;
    public string DeviceModel;
    public string DeviceBrand;
}

public class LoginRequest
{
    public Login login;
    public string DeviceModuleID;
    public string applicationType;
    public string deviceNickname;
    public string version;
}


Now for your HTTP Operation we will change the request type from string to LoginRequest. We will also comment out the attribute on the response field to avoid exceptions on the response... until we know what to model our response after. This is documented here: http://unity3dassets.com/modeling-wak-responses/. And we might as well remove the Uri field and just put the absolute path in the HttpPath attribute.

Code: Select all
 [HttpPOST]
    [HttpPath(null,"http://your.domain.com/endpoint/LoginUser")]
    [HttpContentType("application/json")]
    [HttpAccept("application/json")]
    [HttpTimeout(10f)]
    public class Login : HttpOperation
    {
        //[HttpResponseJsonBody()]
        public string Respnse;

        [HttpRequestJsonBody()]
        public LoginRequest JsonResqust;
}


And lastly we can create the HTTP call in a MonoBehaviour:

Code: Select all
void Start()
        {
    new EvritOperations.Login() {
            JsonResqust =  new LoginRequest( {
                  DeviceModuleID = "8",
                  applicationType = "EvritForChildren",
                  deviceNickname = "deviceNickTest",
                  version = "6.0",
                  login = new Login() {
                     username = _loginView.GetUserName(),
                     password = _loginView.GetPassword(),
                     DeviceSerialNum = "TEST13-635B-4CE3-8A0B-C13A65DD85F0N",
                     DeviceModel = "None",
                     DeviceBrand = "None" }
           })}
        .Send();
}
User avatar
chris
Site Admin
 
Posts: 97
Joined: Thu Feb 28, 2013 1:40 am
Location: Chicago, IL

Re: Post a Json

Postby MrButterFingers » Sun Jan 07, 2018 10:25 am

Thanks,
that solved my problem
MrButterFingers
 
Posts: 3
Joined: Wed Dec 06, 2017 2:27 am

Re: Post a Json

Postby chris » Sun Jan 07, 2018 11:39 am

Cool, let me know if you need more help.

If you can, please leave a rating and a review on the Asset Store!
User avatar
chris
Site Admin
 
Posts: 97
Joined: Thu Feb 28, 2013 1:40 am
Location: Chicago, IL


Return to Web API Kit: Core

Who is online

Users browsing this forum: No registered users and 1 guest

cron