Yak Attack
Yak Attack

Reputation: 105

Dealing with a large quantity of data with a REST web service

I am trying to create a web service which takes a number of arrays (of type double) and returns the mean value of each of these arrays.

To get started I have written a PHP script which retrieves the array using the HTTP GET method and calculates its mean, returning it in the response in JSON format:

<?php
header("Content-Type:application/json");
if(!empty($_GET['array'])) {
    //calculate mean of each array and send results
    $array = $_GET['array'];
    $mean = array_sum($array)/count($array);
    respond(200,"OK",$mean);
} else {
    //invalid request
    respond(400,"Invalid Request",NULL);
}

function respond($status,$message,$data) {
    header("HTTP/1.1 $status $message");
    $response['status']=$status;
    $response['message']=$message;
    $response['data']=$data;
    $json_response=json_encode($response);
    echo $json_response;
}
?>

However, I realized very quickly that with a large array (say, 15000 values) my query string used to pass the values will become so large as to be impractical, if it even works (I have read that many systems place a limit of ~2000 characters on the length of a URL). So, I am looking for a different way to pass the data to my service that will not be limited in this way. The arrays will be generated by a client program. I am using the latest XAMPP stack for the server.

Upvotes: 0

Views: 63

Answers (3)

devlin carnate
devlin carnate

Reputation: 8612

I'm assuming you have control over the client program, in which case, change the client to use HTTP Request, with the request content in the body.

If you don't have control over the client program, you are out of luck. If the client sends the request in $_GET, you must receive it that way.

Upvotes: 0

Hawkings
Hawkings

Reputation: 533

You can use POST instead of GET for that. The way to do that deppends on the language of your client. For example, using jQuery:

$.post('http://yourserver.com', yourArray, function (data, status) {
    //handle the response of the server
});

Upvotes: 1

kungphu
kungphu

Reputation: 4859

For the data you're talking about, you're right, GET is not the appropriate way to send it. Use POST.

Upvotes: 1

Related Questions