1
votes

in my service i have the following request:

[Route(@"/api/adddevice", Verbs = "POST")]
public class AddDeviceRequest : IReturn<AddDeviceResponse> 
{
    public DTOTargetDevice TargetDevice { get; set; }
}   

It uses the following DTO:

public class DTOTargetDevice
{
    private string _guid = string.Empty;
    public string GUID
    {
        get { return _guid; }
        set { _guid = DTOUtils.GetGUID(value); }
    }

    public string Type { get; set; }
    public string DeviceName { get; set; }
    public string[] PropertiesName { get; set; }
    public string[] PropertiesValue { get; set; }
}

From a C# client, everything works fine, but when i try to call it, TargetDevice is always null. This is the javascript code i'm using:

        var parms = {};
    parms.targetDevice = {};
    parms.targetDevice.guid=guid();
    parms.targetDevice.deviceName = guid();
    parms.targetDevice.type = sDeviceType;
    parms.targetDevice.propertiesName = (propsName);
    parms.targetDevice.propertiesValue = (propsValue);
    var config = {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
        }
    }
    var promise = $http.post("http://" + sIP + ":20001/api/adddevice", parms, config).then(function (response) {
        var result = response.data.result;
        console.log("Added Device: " + result);
        return result;
    });

This is the json, taken from Chrom Developer window:

{"targetDevice":{"guid":"e7461703-1b4b-7cd3-6263-3ac0935fb6f7","deviceName":"11e08b8f-d030-8f69-a469-4a1300aa49bf","type":"HTTPDevice","propertiesName":["Name","Acronym"],"propertiesValue":["Device","Device"]}}:

I've tried several options, but i'm always receiveng a null exception on TargetDevice within the request.

I've already read in other threads that simple objects is preferred, but in other services i have even more complex DTO. Do you have any suggestion about that?

Thanks Leo

2

2 Answers

3
votes

I am quoting you:

This is the json, taken from Chrom Developer window:

The important word from your sentence is this: json. So make sure you specify the correct content type when making your request. You need to be consistent in what you claim to be sending and what you are actually sending to the server:

var config = {
    headers: {
        'Content-Type': 'application/json'
    }
};

In your example you were claiming that you were sending application/x-www-form-urlencoded;charset=utf-8; and then dumping a JSON string, how do you expect the server to be able to cope with this confusion?

I guess that Angular or whatever this $http.post function that you are using will automatically JSON.stringify the parms object before sending it to the server. Maybe it will even automatically add the application/json content type request header, so you could completely get rid of this config variable of yours in this case.

1
votes

Solved. Instead of using in SetConfig()

                GlobalResponseHeaders =
            {
                { "Access-Control-Allow-Origin", "*" },
                { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
                { "Access-Control-Allow-Headers", "Content-Type" },
            },

I've used:

Plugins.Add(new CorsFeature());

Thanks guys! LEo