0

I am trying to post to a WCF json service endpoint from angular but have been unsuccessful in my attempts. I have verified the service is working by other means and working for the specified URL.

Using firebug I can see the request being output as such:

NetworkError: 400 Bad Request - http://www.myapi.com/V1/Service.svc/Authenticate?Password=password&UserName=username"

angular code

app.service('UserService', function ($http) {
this.GetLoginStatus = function (AuthenticateRequest) {
    $http({
        url: APIURL + "/Authenticate",
        method: "POST",
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        params: AuthenticateRequest,
        data: {
            'Code': 'test data'
        }
    });
};

});

WCF Iservice

[WebInvoke(RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped)]
[OperationContract]
AuthenticateResponse Authenticate(AuthenticateRequest Request);

Request definition

[DataContract]
public class AuthenticateRequest
{
    [DataMember]
    public String UserName { get; set; }
    [DataMember]
    public String Password { get; set; }
}
3
  • Where do you setup/define the basic auth on your service? You are sure the auth string is good? Do you use postman in chrome? Commented Apr 17, 2014 at 2:55
  • I updated my code and removed that line. I did use postman. Commented Apr 17, 2014 at 3:17
  • I'm not extremely familiar with WCF, but isn't the wrapped() style for adding XML elements? If the intention is for the api to just speak json, maybe you do not want that? Also, I can't help but think you might want to send the content in the body, in the data property and not as query params (as the service is expecting POST). Commented Apr 17, 2014 at 3:24

1 Answer 1

1

I managed to get this done by stringfy-ing a javascript object.

    Service.js

        angular.module('myModule').factory('serviceFac', ['$http', '$q', 
function (a, b) 
    {   var taskMergeServiceNameSet = "WebServuice.svc/Tasks/SetTasks";
            return {
                setTasksMerge: function (taskIds, action) {

                 var objArray = '';
                 var obj = {
                        taskIds: taskIds,
                        action: action,
                        userName: "ss"
                  };
                 objArray = new Array();
                 objArray.push(obj);
                 var deferred = b.defer();
                 a({
                   url: taskMergeServiceNameSet,
                   method: 'POST',
                   headers: { 'Content-Type':'application/json; charset=utf-8'},
                   data: JSON.stringify(objArray)
                  }
                  ).sucess(function (data) { deferred.resolve(data) })
                  .error(function (msg, code) { deferred.reject(msg) });    
                    return deferred.promise;
                }
            }
        }]);

Service Contract

ServiceContract Interface

[ServiceContract]
public interface ITasks
{
   [OperationContract]
    [WebInvoke(Method = "POST",RequestFormat=WebMessageFormat.Json ,
      ResponseFormat =   WebMessageFormat.Json, 
      UriTemplate = "Tasks/SetTasksForMerge")]
    string CreateNewTaks(valObj[] values);        
}

[DataContract]
public class valObj
    {
    [DataMember]
    public string taskIds { get; set; }
    [DataMember]
    public string action { get; set; }
    [DataMember]
    public string userName { get; set; }
    }

post here helped me a lot. please let me know if you suceed in passing a JSON string

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.