local_klaus
local_klaus

Reputation: 11

API POST PHP - 415 Unsupported Media Type (Zend Framework)

I have an issue POSTing data.I getting as an anweser "415 Unsupported Media Type". I can GET data but if I POST I get as an anweser "415 Unsupported Media Type".

Any help are welcome!

Below you can see the bad request and the HEADER and POST:

Anweser:

HTTP/1.1 400 Bad Request X-content-type-options: nosniff X-xss-protection: 1; mode=block Pragma: no-cache X-frame-options: DENY Set-cookie: HTTP ERROR: 400


Header:

$httpClient -> setHeaders(array(

            "Content-Type : application/json; charset=utf-8",
            "Accept : application/json",
            "SM_USER :". $authToken,
            "Accept-Language : de"

    ));

POST,GET:

    switch($requestType) {

        case "get":

            $restRes = $this->restClient->restGet($path,$queryParameter);
            $result = $this->_handleRestResult($restRes);
            return $result;
            break;

        case "post":

           $restRes = $this->restClient->restPost($path,$queryParameter);

            break;

        case "delete":

        case "put":

    }

QUERYPARAMETER

 $query = json_encode(array(

            "latitude" => $lat, 
            "longitude" => $long,
            "service" => $service

    ));

PATH

 $path = $this-service->rest->path."/resource/Service/";

Upvotes: 0

Views: 869

Answers (1)

local_klaus
local_klaus

Reputation: 11

We can not use $this->restClient->restPost, because it will reset the whole underlying Http_Client again! Which specifically means it resets the headers we used. So we do all the steps by ourselves.

switch($requestType) {


    case "post":

       $httpClient->setUri($this->_config->service->rest->host."".$path);
                $httpClient->setMethod('POST');
                $httpClient->setRawData($queryParameter, $httpClient->getHeader('Content-Type'));    
                $restRes = $httpClient->request($method);

           $this->restClient->getHttpClient()->getHeader('Content-Type'));
            print_r($this->restClient->getHttpClient()->getHeader('Accept'));
            print_r($this->restClient->getHttpClient()->getLastRequest());
            print_r($restRes);
            print_r($this->restClient->getHttpClient()->getLastResponse());

             $result = $this->_handleRestResult($restRes);
             return $result;
             break;
        break;

    case "delete":

          //

    case "put":

          //

    default:
    case "get":

        $restRes = $this->restClient->restGet($path,$queryParameter);
        $result = $this->_handleRestResult($restRes);
        return $result;
        break;
}

Upvotes: 1

Related Questions