0

I made a custom REST API on Magento 2. I want some params to be required and other to be no required or accept null. I have the next code:

Api/Order/ShippingInterface

  /**
* API00004. Envío de Pedido
* Url Path: /rest/V1/orders/sap/shipping
* Se mandará a este servicio la información cuando un pedido ya ha
* sido enviado.
*
* @api
* @param string IdPedido
* @param string ControlNumber
* @param string UrlFile
* @param string DecodeFile
* @param string[] Trackings
* @return string JSON con Mensaje de Confirmación o Error.
*/
public function set(
  $IdPedido = null,
  $ControlNumber = null,
  $UrlFile = null,
  $DecodeFile = null,
  $Trackings = null
);

Model/Api/Order/Shipping.php

/**
* API00004. Envío de Pedido
* Url Path: /rest/V1/orders/sap/shipping
* Se mandará a este servicio la información cuando un pedido ya ha
* sido enviado.
*
* @api
* @param string IdPedido
* @param string ControlNumber
* @param string UrlFile
* @param string DecodeFile
* @param string[] Trackings
* @return string JSON con Mensaje de Confirmación o Error.
*/
public function set(
  $IdPedido = null,
  $ControlNumber = null,
  $UrlFile = null,
  $DecodeFile = null,
  $Trackings = null
){ ... }

But when I send a Json like:

{"IdPedido":null,"ControlNumber":null,"UrlFile":null,"DecodeFile":null,"Trackings":null}

It returns me the next error:

message":"One or more input exceptions have occurred.","errors":[{"message":"%fieldName is a required field.","parameters":{"fieldName":"IdPedido"}},{"message":"%fieldName is a required field.","parameters":{"fieldName":"ControlNumber"}},{"message":"%fieldName is a required field.","parameters":{"fieldName":"UrlFile"}},{"message":"%fieldName is a required field.","parameters":{"fieldName":"DecodeFile"}},{"message":"%fieldName is a required field.","parameters":{"fieldName":"Trackings"}}]

How can I do this?

Thanks.

1
  • Please post a code of your webapi.xml file. – Aditya Shah Oct 2 '18 at 4:50
1

Open file

/vendor/magento/module-checkout/etc/webapi.xml

and inside try to locate this code

<route url="/V1/carts/mine/shipping-information" method="POST">
    <service class="Magento\Checkout\Api\ShippingInformationManagementInterface" method="saveAddressInformation"/>
    <resources>
        <resource ref="self" />
    </resources>
    <data>
        <parameter name="cartId" force="true">%cart_id%</parameter>
    </data>
</route>

You see, specifically inside the <data> tag, the <parameter> tag has attribute named force and it is equal to true in this case.

I'm inclined to say that this is the way do to it, in your case you will of course set force="false". Though, I have never tried this myself to be honest.

Anyway, open this link

https://devdocs.magento.com/guides/v2.2/extension-dev-guide/service-contracts/service-to-web-service.html

and further click on Sample webapi.xml file

and I think you will find similar recommendations of XML handling this.

I hope this help, good luck.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.