Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following web method in my web api controller

    public HttpResponseMessage PostMakeBooking(FacilityBookingRequest bookingRequest)
    {

        var returnStatus = HttpStatusCode.OK;
        var json = new JavaScriptSerializer().Serialize(bookingRequest);

        var response = Request.CreateResponse<CardholderResponse>(returnStatus, cardholderResponse);


        return response;

    }

When I make this call from my .NET app, my json string appears correctly when I seralize it

{"correlationId":null,"RequestId":"7ec5092a-342a-4e32-9311-10e7df3e3683","BookingId":"BK-123102","CardholderId":"123456","BookingFrom":"\/Date(1370512706448)\/","BookingUntil":"\/Date(1370523506449)\/","DeviceId":"ACU-01-R2","Action":"Add","LoginId":"tester","Password":"tester"}

However, when I made to call from my php script

public function web_request(){

    $guid   =self::getGUID();
    $replace = array("{","}");
    $guid  = str_replace($replace, "", $guid);

    $client = new Zend_Rest_Client("http://203.92.72.221");
    $request= new myZendCommon_FacilityBookingRequest();
    $request->RequestId         =$guid;
    $request->BookingFrom       ="27/03/2013 05:30";
    $request->BookingUntil      ="27/03/2013 06:30";
    $request->CardholderId      ="E0185963";
    $request->DeviceId          ="ACU-B2-01-R1";
    $request->BookingId         ="111";
    $request->Action            ="Add";
    $request->LoginId           ="tester";
    $request->correlationId     ="(null)";
    $request->Password          ="tester";


    $request = json_encode($request);

    $response = $client->restPost("/ibsswebapi/api/facilitybooking",$request);


    print_r($response);
    exit();

The call goes to my web method, but when I serialize it using JavaScriptSerializer().Serialize(bookingRequest)

{"correlationId":null,"RequestId":null,"BookingId":null,"CardholderId":null,"BookingFrom":"\/Date(-62135596800000)\/","BookingUntil":"\/Date(-62135596800000)\/","DeviceId":null,"Action":null,"LoginId":null,"Password":null}

All the values are null.

Is something wrong with the script?

share|improve this question

2 Answers

  1. Are you sending the header Content-Type:application/json in your request?

  2. Also add the following piece of code to catch any model state validation errors:

    if (!ModelState.IsValid) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState)); }

share|improve this answer

I believe Kiran is right. Not sure why some one has felt his answer is not useful. Anyways, my understanding is that you are creating a JSON string and doing a form post of the same. I guess in this case the content type is sent as application/www-form-urlencoded but request body is a JSON string. You can use Fiddler to see how the request is being sent by the PHP script. I don't have the PHP knowledge to tell you how you can post JSON but my guess is that if you just remove the JSON encoding line $request = json_encode($request);, it should be okay.

From ASP.NET Web API point of view, if the request has Content-Type: application/json header and the body has the right JSON or if the request has Content-Type:application/www-form-urlencoded header and the body has the form url encoded content like RequestId=7ec5092a-342a-4e32-9311-10e7df3e3683&BookingId=BK-123102 and so on, web API will absolutely have no problem in binding. Currently, the request is not being sent in the right format for web API to bind.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.