B2W API REST/JSON -failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized

Asked

Viewed 835 times

0

I’m starting an integration with B2W, they use a REST/JSON API, I am not finding where is wrong in my code, it is something related to the header, because I test with the Chrome dhc add-on and records the change correctly: inserir a descrição da imagem aqui

my code:

$url = 'https://api-sandbox.bonmarketplace.com.br/sku/10205_SKU1/price';

$data = json_encode(array(
            "sellPrice"=> "400.00",
            "listPrice"=> "400.00"
            )
    );  
//echo "<br>".$usuario = base64_encode("$username:");
//Content-Type:application/json;charset=utf-8;

$options = array(
    "http" => array(
        "method" => "PUT",
        "header" => "Content-Type: [application/json; charset=UTF-8; Authorization: Basic NjU5NjdGQkZBMDEzNjUwMTkyNzc1OTQ5MDI2NjUzNEU6",
        "content" => $data
));
print_r($options);
$context = stream_context_create($options);
print_r($context);
// make the request
$response = file_get_contents($url, false, $context);

Error 401 Unauthorized

  • This json ai can be a common php array, when sending vc calls json_enconde().

  • Yes, I’ve even built it into array to use the function, but my question is on the structure of the code on how to call the API using post and pass this ARRAY

  • tried sending a hash with random md5 to see if the problem isn’t caching the process output?

  • Hi Ivan, how would this implementation? Can you help me?

1 answer

1


I am not an expert in PHP, but as URL calls I only know how to do with Curl, here is an example that does not return any error

<?php
    $url = 'http://api-sandbox.bonmarketplace.com.br/sku/10205_SKU1/price';
    $data = json_encode(
               array(
                  "sellPrice"=> "400.00",
                  "listPrice"=> "550.00"
               ));  
?>
<?php
    // usando cURL
    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "PUT",
      CURLOPT_POSTFIELDS => $data,
      CURLOPT_HTTPHEADER => array(
        "authorization: Basic NjU5NjdGQkZBMDEzNjUwMTkyNzc1OTQ5MDI2NjUzNEU6",
        "content-type: application/json;charset=UTF-8"
      ),
    ));

    $chleadresult = curl_exec($curl);
    $chleadapierr = curl_errno($curl);
    $chleaderrmsg = curl_error($curl);
    curl_close($chlead);
?>
<hr>
<h2>$chleadresult</h2>
<pre><?php echo $chleadresult ?></pre>
<h2>$chleadapierr</h2>
<pre><?php echo $chleadapierr ?></pre>
<h2>$chleaderrmsg</h2>
<pre><?php echo $chleaderrmsg ?></pre>

but I think the real problem is the validation of header, because the code it shows should be written as:

$options = array(
    "http" => array(
        "method" => "PUT",
        "header" => array(
            "Content-Type: application/json; charset=UTF-8",
            "Authorization: Basic NjU5NjdGQkZBMDEzNjUwMTkyNzc1OTQ5MDI2NjUzNEU6"
        ),
        "content" => $data
));

By the way, if you use the tool Postman (free), as soon as you make one request, has available this request in a variety of languages, including 3 versions of PHP:

inserir a descrição da imagem aqui

  • perfect @balexandre was the simple array in header, a crossbow error that consumed many neurons, thanks for the help and also by Postman will be very useful

Browser other questions tagged

You are not signed in. Login or sign up in order to post.