简体   繁体   中英

Calling ASP.NET Web Api method with PHP

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?

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.

  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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM