0

I have created a REST API using Magento 2.3.2 CE

All went well, but now I want to return custom HTTP Status Code with my error message like 401 Unauthorized, 409, 400, etc. Currently, for all my API calls, I am getting HTTP Status Code 200

I need to pass custom response codes, how can I achieve this in Magento2 ?

Similar question has been asked for PHP here. I am facing exactly same issue in Magento2

This is how I am returning response:

public function getRequest($request)
{
    $response = $this->orderApiFactory->create();

    /*
     * Dummy Response
    */
    $response->setCode(400);
    $response->setMessage('Failed to complete your request. Please try again later!');
    $response->setOrderId('');
    return $response;
}

The response from curl_get_info($ch) I am getting:

array (size=26)
  'url' => string 'http://example.local/index.php/rest/V1/reg-renewal/createOrder' (length=62)
  'content_type' => string 'application/json; charset=utf-8' (length=31)
  'http_code' => int 200
  'header_size' => int 542
  'request_size' => int 524
  'filetime' => int -1
  'ssl_verify_result' => int 0
  'redirect_count' => int 0
  'total_time' => float 0.648522
  'namelookup_time' => float 1.4E-5
  'connect_time' => float 5.5E-5
  'pretransfer_time' => float 8.8E-5
  'size_upload' => float 321
  'size_download' => float 95
  'speed_download' => float 146
  'speed_upload' => float 495
  'download_content_length' => float 95
  'upload_content_length' => float 321
  'starttransfer_time' => float 0.648498
  'redirect_time' => float 0
  'redirect_url' => string '' (length=0)
  'primary_ip' => string '127.0.0.1' (length=9)
  'certinfo' => 
    array (size=0)
      empty
  'primary_port' => int 80
  'local_ip' => string '127.0.0.1' (length=9)
  'local_port' => int 42144

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.