.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Post New Web Links

How to Catch Bad JSON Request Payload in REST using WCF3.5

Posted By:      Posted Date: September 10, 2010    Points: 0   Category :WCF
 
Hi, I'm using WCF 3.5 REST with JSON output. If I send the below request body {"persons": [{"fname":"John"}, {"fname":"sheena"}} to my web API, my REST service throws "500 - Internal Server Error". If you look at carefully, request is missing the ending square bracket & hence the error comes. Now, I would like to know how to catch this error??? So, far I've no luck in catching this error at all. I need to give 400 Bad Request error instead. How do I do that? Thanks!


View Complete Post


More Related Resource Links

wcf 4 rest service accepts xml and json posts but get 400 bad request on form post

  

Using the rest 4 template.  I have a POST method defined, with an object as a parameter.  REST 4 does a great job of automatically mapping both xml and json posts to the method.  I tested this using an HttpClient test application to build and post xml content and then json content.  All data gets correctly mapped to the corresponding properties in my entity. 

I want to also support a standard form post to the method but am having problems! 

I'd like the same method already accepting and converting both xml and json to also work correctly for a form post.  I figured it would "just work" but no.

I am testing this by creating an HttpUrlEncoded form in the test client, adding form name/value pairs that match what the method is expecting, and trying to post it.

However I get a 400 Bad Request.  Here's some example code that is returning a 400 bad request:

Really straightforward but fails.  Ideas why? 

 

      var client = new HttpClient();

      HttpUrlEncodedForm form = new HttpUrlEncodedForm();
      form.Add("Id", "99"<

PUT method gives error 400 "Bad Request" in WCF REST Service Application

  
I am trying to learn WCF REST Services through http://blogs.msdn.com/endpoint/archive/2010/01/14/updating-state-in-wcf-webhttp-services.aspx , It's a link of Part 3 of 12 part blog series. Now I am trying the same stuff given in the part 3 with my own Entities and Entity Model instead of what is given in the blog. Here is my full project I am working on .. http://cid-9e3f62c0688824b0.skydrive.live.com/self.aspx/.Public/pronerveEPMS.zip For comparision with the code shown in the Blog I am following, you can download the zip file of the project from the same link given here at starting (At the end of the blog and before comments starts). And here is the method in Program.cs file of console project in my application that raises the exception.....        private static Role UpdateRole(HttpClient hc, Role role1)        {            Console.WriteLine("Updating Role {0} : ",     role1.RoleID);            Console.WriteLine();            string updateURI = "Roles/" + role1.RoleID.ToString();            HttpContent content = HttpContentExtensions.CreateDataContract(role1);            using (HttpResponseMessage response = hc.Put(upd

Obtain url query string during Json request

  
So I'm writing an MVC application, and have a JsonResult post back to obtain some item information for scanned items on a warehouse application I am writing. I'm trying to obtain a URL query string value though, and cannot seem to obtain one via the normal 'Request.QueryString['variable']' way... Is there a way to do this? I also thought about just passing the parameter into my post, but I already have a single value on my jquery post, and I am unsure if I can tack on more values to this. Thanks for your help.

Simple JSON request

  

Hi,

I'm trying to make API calls to a service provider. I've never tackled JSON requests before but I want to give it a go - as they seem less verbose than XML. Could someone be kind enough to give me a simple example here? 

My confusion stems from some libraries I've come across. Do I need to add any libraries to my project? Or is it just a matter of creating a string that complies with JSON syntax and send it via a WebRequest?


WCF REST IIS 7 bad request 400

  

tried so many different things but havent got past this seemingly trivial problem.

I created a WCF service application from visual studio 2010 WCF templates. it generated the IService.cs and Service1.svc files and generated the web.config

i created a website in IIS 7 on windows 2008, and pointed it to the app folder. when i go to http://service.myhost.com/service1.svc via web browser, i get a message indicating service is running and suggests using svcutil etc etc., but when i go to http://service.myhost.com/service1.svc/getdata?value=10 the web browser says 400 bad request. this is without changing anything from what VS generated. the operation contract looks like

[OperationContract]
string GetData(int value);

and the proxy looks like

public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}

web .config looks like

<?xml version="1.0"?>
<configuration>

<system.web>
<compilation debug="true" targetFramework="4.0" />

How WCF 4 REST Service Support Form Post As Well As XML and JSON?

  

Using the rest 4 template.  I have a POST method defined, with an object as a parameter.  REST 4 does a great job of automatically mapping both xml and json posts to the method.  I tested this using an HttpClient test application to build and post xml content and then json content. 

I want to also support a standard form post to the method but am having problems!  

I'd like the same method already accepting and converting both xml and json to also work correctly for a form post.  I am testing this by creating an HttpUrlEncoded form in the test client, adding form name/value pairs that match what the method is expecting, and trying to post it.

However I get a 400 Bad Request.  Here's some example code that is returning a 400 bad request:

            var client = new HttpClient();

            HttpUrlEncodedForm form = new HttpUrlEncodedForm();
            form.Add("Id", "99");
            form.Add("Message", "Message test with 'special' chars #!#");
            form.Add("IpAddress", "192.168.1.1");

            var response = client.Post("<valid-uri-here>", form.CreateHttpContent());
            response.EnsureStatusIsSuccessful();

            client.Disp

catch http request in javascript

  

Hi is there a way i can catch http requests? i would like to fire some javascript the will display a loading message anytime http request is made. the updateprogress is no good as i am using jquery.

i am sure there is a way to do this?


WCF REST Catch Exception ClientSide

  

I am using WcfRestContrib which allows me to throw a WebException from the server (among other things). Very Similar to how the Rest Starter Kit allows with WebProtocolException. This sets the proper return code and a helper string. (this is visible in a browser so I know it does work)

I am using the WebChannelFactory for the client side. What I would like to do is try to catch this message on the client side but am unable to do so. The client receives a ProtocolException with a brief explanation about the error code.

I added a custom Behavior to the client side to add a message inspector but the exception is thrown before the AfterReceiveReply call is made. How can I accomplish what I am looking for?

Please Help! Thanks!


Perform a Get Request, and reading JSON request

  

Hey,

I need to perform a Get request to a URL that will take 2 parameters, which will return to me a JSON response based on those parameters.

The response will look like this {"status":"OK","url":"http://www.link.com","name":"Rathfarnham"}

Im Using ASP.NET 2, and wondering how to best

1) Perform the GET Request to the URL 2) Read the JSON response

Cheers


.net 2010 REST WCF - json does not return the root List type

  

Hi,

I am using the .net 2010 WCF Rest Service Application template to create my services.

The service needs to support two return format - xml and json which 2010 easily handles by changing the Accept value in the header info.

I have the following class structure

   public class ri
    {
        public string nm { get; set; }

        public string nval { get; set; }

        public string cnt { get; set; }
    }

    public class r
    {
        public string val { get; set; }

        public string nm { get; set; }

        public List<ri> ri { get; set; }
    }

   public class refinements
    {
        public List<r> refs { get; set; }
    }

   public class GetRefinementsSvc
    {
        [WebGet(UriTemplate = "")]
        public refinements GetCollection

Design Data Contract for User Create/Update/Delete REST API using WCF 3.5 & JSON

  

Hi,

I'm using REST with WCF .NET 3.5. I've design the class structure with below JSON struvture of input & output.

API 1: Create User: POST /id & request body is as shown below:

{
  "userList" :
   [
    {
       "name" : "<name>",
       "email" : "<email>",
       "dataId" : <dataId>,
       "country" : "<country code>",
    },
    {
       "name" : "<name>",
       "email" : "<email>",
       "dataId" : <dataId>,
       "country" : "<country code>",
    }
   ]
}

The response object should be as shwon below:

{
  "userCreateResults" :
   [
    {
       "email" : "<email>",
       "userId" : <userId>,
       "password" : "<password>",
       "result" : <result code>
    },
    {
       "email" : "<email>",
       "userId" : <userId>,
       "password" : "<password>",
       "result" : <result code>
    }
   ]
}

API 2: Update User: PUT /id & request body is as shwon below:

{
  "userList" :
   [
    {
       "userI

How to read web.config params in WCF 3.5 with REST using JSON

  

Hi Carlos,

I've a web site project on top of which I've created my REST service by creating a New Project -> New Web Site instead of WCF Service. The reason I did this is because I wanted to use some config params from original web site's web.config

Is there any way to read the web site's web.config if I chose the option New Wcf Service?

Thanks!


REST WCF 3.5 using JSON - Default Value issue & EmitDefaultValue colliding with each other

  

Hi Carlos,

This question is related to forum http://social.msdn.microsoft.com/Forums/en/wcf/thread/1698968e-6608-485d-a8d5-d4c0138c1078

In case Update API, if I pass data Id as 0, I'm having issue since it is the default value of int & dataId is declared as int. Similarly, result is declared as int. Because of this, if I want to pass 0 in result value instead of 200, its not returning me the value at all as it is declared as int & its emitdefaultvalue is set to false.

Can you tell me if there is any way to solve this issue? Am I suppose to declare all variables as string in the data contract so that I won't have this issue of default values or is there any other way?

Thanks!


GetUser by UserId or Email - REST .NET with JSON

  

Hi,

For GetUser API referenced in http://social.msdn.microsoft.com/Forums/en/wcf/thread/1698968e-6608-485d-a8d5-d4c0138c1078, lets say that I've a requirement where I need to get the user either by userId or by email.

Now, if user invokes this API by passing name instead of userId or email, they should get error, now may I know how can be it done using REST .NET? I cannot set IsRequired to true to both userId & email, right?

If this is not possible using .NET, its another drawback of .NET!!!

Please help me out!

Thanks!


SQL Server 2005 Try and Catch Exception Handling

  
I'm pretty excited to see that there is some real error handling for T-SQL code in SQL Server 2005. It's pretty painful to have your wonderfully architected .NET solution tainted by less-than-VBScript error handling for stored procedures in the database. The big difference being the addition of TRY..CATCH blocks. Let's take a look:

FORMAL REQUEST TO THE MICROSOFT DYNAMIC DATA TEAM

  

Dear gentlemen and ladies of the Microsoft Dynamic Data Team,

Mr. Steve Naughton says (and I hope so) that there might be chances that you read the posts published in this forum: I really hope so.

If that were the case then I am formally requesting to you in providing a "real answer" to my question:

I want to customize a dynamic data page (the details one) so I can have access to the data it has been already gathered from the end user UI (details.aspx) and use it to pass this same data to the following controls/pages that I am going to display next time to the end user.

For instance:

I have a screen with different types of data in it: texts, numbers, dates, check boxes, radio buttons and dropdownlists that has been filled with data by the end user. I want now to reset all fields exept the dropdownlist of which I'd like to keep the previously chosen values(the very same thing you do between dropdownlist of List.aspx ad Details.aspx). Even with FoxPro I could do that because data and controls were available there. It was as simple as setting carrying on to true.

HOW DO YOU DO THAT? HOW CAN I IMPLEMENT THE SAME LOGIC AMONG THE CONTROLS OF MY DETAILS PAGE? BUT I DO WANT TO HAVE ACCESS AND PASS DATA FOR SPECIFIC CONTROLS:DROPDOWNLISTS (mostly).

I have found my

view state vs request parameters

  

If  the view state is the data entered into the form fields then they are supposed to be available in the request parameters or request body. Then why would we need view state for?


Thanks.



Categories: 
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend