Invalid Content-Type posting wtih zend http

Can anyone tell me why I keep getting an invalid Content-Type with the following code. Thanks in advance

$request = new Laminas\Http\Request();
$request->getHeaders()->addHeaders(array(
    'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
    'Accept' => "*//*",
    
));
$request->getPost()->set('name', 'Test Name');
$request->setUri($uri);
$request->setMethod('POST');

$client   = new Laminas\Http\Client();
$response = $client->send($request);

This part is wrong and not needed: ; charset=UTF-8

Reference: https://url.spec.whatwg.org#urlencoded-parsing

thanks but that doesn’t make a difference and works with all my put and delete requests with that header. I am not sure if i am passing my parameters correctly and have seen people use the setRawbody but i can’t get that to work either.

Thanks again for trying to help though. good to see some life here :slight_smile:

You should also recheck the “Accept” header line. The value for any MIME type looks different.

I will check this and give you a feedback.

Two examples to try out.

Usage only with client

// Create client
$client = new Laminas\Http\Client('https://duckduckgo.com');
$client->setMethod(Laminas\Http\Request::METHOD_POST);
$client->setHeaders([
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Accept'       => '*/*',
]);
$client->setParameterPost([
    'q' => 'laminas',
]);

// Send
$response = $client->send();

// Result
var_dump($response->isOk()); // true
var_dump($response->getStatusCode()); // 200

echo $response->getBody(); // <!DOCTYPE html><html lang="en_US" class="no-js has-zcm  no-theme  is-mobile-header-exp"><head><meta http-equiv="content-type" content="text/html;…

Usage with custom request

// Create requesz
$request = new Laminas\Http\Request();
$request->setUri('https://duckduckgo.com');
$request->setMethod(Laminas\Http\Request::METHOD_POST);
$request->getHeaders()->addHeaders([
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Accept'       => '*/*',
]);
$request->getPost()->fromArray([
    'q' => 'laminas',
]);

// Create client
$client = new Laminas\Http\Client();

// Send
$response = $client->send($request);

// Results
var_dump($response->isOk()); // true
var_dump($response->getStatusCode()); // 200

echo $response->getBody(); // <!DOCTYPE html><html lang="en_US" class="no-js has-zcm  no-theme  is-mobile-header-exp"><head><meta http-equiv="content-type" content="text/html;…

Maybe it will help to find your problem.

froschdesign, thanks for the help. your first example gives me the same invalid content type and the second example give me the validation failed name is null.

Please keep in mind: We know nothing and see nothing of your application. Therefore it is difficult to say where the error comes from and what the problem is.
At the moment we only see that you want to send data via POST. We do not see anything from the recipient. Therefore you should also check the other side.

Another option: use your IDE to create the HTTP request and check the results.

I can create the request fine using postman or curl but I would like my app to be clean and use laminas\Http for all of the requests. I am calling a standard rest service from api-tools.

Here is the result:
Array ( [type] => http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html [title] => Unsupported Media Type [status] => 415 [detail] => Invalid content-type specified )

With the same header line for “content-type”?

in postman I used the same content-type in curl it doesn’t even send one.

This is it done using curl where data is just an array with name => ‘test name’

 $curl = curl_init();
        
        curl_setopt_array($curl, array(
            CURLOPT_URL => $this->_uri,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $data,
        ));
        
        $response = curl_exec($curl);
        
        curl_close($curl);
        return $response;

It’s quite confusing. I think you should recheck if the content type application/x-www-form-urlencoded is correct and needed here. Or some different content type must be used.

Do you mean “Laminas API Tools”?

yes I do. I did more testing with this earlier and using the examples in the Laminas http documentation didn’t send the post data like their example showed.

Thanks again for your help

Check the content type you are passing to the request. You can use postman to first check what content type the API request first before you make a request to it.
If you don’t know how, this man has a postman tutorial that can help you.