Magento Forum

API REST, message error ( Content-Type header is empty )? 
 
5imix4kankin
Jr. Member
 
Total Posts:  3
Joined:  2010-08-18
 

I’m testing API REST - magento 1.7.0.2
oauth authentication: ok
all rest GET response: ok
all rest POST response result in error:
{\"messages”:{\"error”:[{\"code”:400,\"message\":\"Content-Type header is empty\"}]}}

###################################################

I´m use this Example http://www.magentocommerce.com/api/rest/introduction.html
###################################################
<?php
/**
* Example of simple product POST using Admin account via Magento REST API. OAuth authorization is used
*/
$callbackUrl = \"http://yourhost/oauth_admin.php\";
$temporaryCredentialsRequestUrl = \"http://magentohost/oauth/initiate?oauth_callback=\" . urlencode($callbackUrl);
$adminAuthorizationUrl = \’http://magentohost/admin/oauth_authorize\’;
$accessTokenRequestUrl = \’http://magentohost/oauth/token\’;
$apiUrl = \’http://magentohost/api/rest\’;
$consumerKey = \’yourconsumerkey\’;
$consumerSecret = \’yourconsumersecret\’;

session_start();
if (!isset($_GET[\’oauth_token\’]) && isset($_SESSION[\’state\’]) && $_SESSION[\’state\’] == 1) {
$_SESSION[\’state\’] = 0;
}
try {
$authType = ($_SESSION[\’state\’] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();

if (!isset($_GET[\’oauth_token\’]) && !$_SESSION[\’state\’]) {
$requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
$_SESSION[\’secret\’] = $requestToken[\’oauth_token_secret\’];
$_SESSION[\’state\’] = 1;
header(\’Location: \’ . $adminAuthorizationUrl . \’?oauth_token=\’ . $requestToken[\’oauth_token\’]);
exit;
} else if ($_SESSION[\’state\’] == 1) {
$oauthClient->setToken($_GET[\’oauth_token\’], $_SESSION[\’secret\’]);
$accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
$_SESSION[\’state\’] = 2;
$_SESSION[\’token\’] = $accessToken[\’oauth_token\’];
$_SESSION[\’secret\’] = $accessToken[\’oauth_token_secret\’];
header(\’Location: \’ . $callbackUrl);
exit;
} else {
$oauthClient->setToken($_SESSION[\’token\’], $_SESSION[\’secret\’]);
$resourceUrl = \"$apiUrl/products\";
$productData = json_encode(array(
\’type_id\’ => \’simple\’,
\’attribute_set_id\’ => 4,
\’sku\’ => \’simple\’ . uniqid(),
\’weight\’ => 1,
\’status\’ => 1,
\’visibility\’ => 4,
\’name\’ => \’Simple Product\’,
\’description\’ => \’Simple Description\’,
\’short_description\’ => \’Simple Short Description\’,
\’price\’ => 99.95,
\’tax_class_id\’ => 0,
));

//THE POSSIBLE ERROR IS HERE
$headers = array(\’Content-Type\’ => \’application/json\’);

$oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
print_r($oauthClient->getLastResponseInfo());
}
} catch (OAuthException $e) {
print_r($e);
}
###################################################

the possible error is here:
$headers = array(\’Content-Type\’ => \’application/json\’);

response error:
{\"messages”:{\"error”:[{\"code”:400,\"message\":\"Content-Type header is empty\"}]}}

Please, How to solve for this Problem?

 
Magento Community Magento Community
Magento Community
Magento Community
 
flugratte
Jr. Member
 
Total Posts:  1
Joined:  2008-10-04
 

Hi 5imix4kankin,

I am having exactly the same problem. Did you find a solution meanwhile?

Thanks for any advice,
Tobi.

 
Magento Community Magento Community
Magento Community
Magento Community
 
huyleit
Jr. Member
 
Total Posts:  1
Joined:  2011-07-22
 

This’s core bug. Try to change your request header like this:
....
“Content-Type” = “application/json; charset=utf-8”;
“Content_Type” = “application/json; charset=utf-8”;
...

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top