[Python] I want to create a waybill for my customers who ordered from my website with aramex

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    this is the code i use:

    import datetime
    def send_issue_policy(request):

    url = "https://ws.sbx.aramex.net/ShippingAPI.V2/Shipping/Service_1_0.svc/json/CreateShipments"
    headers = {
    "Content-Type": "application/json",
    "Accept": "application/json"
    }

    payload = {
    "ClientInfo": {
    "UserName": "testingapi@aramex.com",
    "Password": "R123456789$r",
    "Version": "v1",
    "AccountNumber": "4004636",
    "AccountPin": "432432",
    "AccountEntity": "RUH",
    "AccountCountryCode": "SA",
    "Source": 24
    },
    "Shipments": [
    {
    "Reference1": "",
    "Reference2": "",
    "Reference3": "",
    "Shipper": {
    "Reference1": "TEst mutli label",
    "Reference2": "",
    "AccountNumber": "4004636",
    "PartyAddress": {
    "Line1": "Test TEST",
    "Line2": "",
    "Line3": "",
    "City": "riyadh",
    "StateOrProvinceCode": "",
    "PostCode": "",
    "CountryCode": "sa",
    "Longitude": 0,
    "Latitude": 0,
    "BuildingNumber": "null",
    "BuildingName": "null",
    "Floor": "null",
    "Apartment": "null",
    "POBox": "null",
    "Description": "null"
    },
    "Contact": {
    "Department": "",
    "PersonName": "aramex",
    "Title": "",
    "CompanyName": "aramex",
    "PhoneNumber1": "966512345678",
    "PhoneNumber1Ext": "",
    "PhoneNumber2": "",
    "PhoneNumber2Ext": "",
    "FaxNumber": "",
    "CellPhone": "966512345678",
    "EmailAddress": "test@test.com",
    "Type": ""
    }
    },
    "Consignee": {
    "Reference1": "",
    "Reference2": "",
    "AccountNumber": "71500004",
    "PartyAddress": {
    "Line1": "Test",
    "Line2": "",
    "Line3": "",
    "City": "riyadh",
    "StateOrProvinceCode": "",
    "PostCode": "",
    "CountryCode": "sa",
    "Longitude": 0,
    "Latitude": 0,
    "BuildingNumber": "",
    "BuildingName": "",
    "Floor": "",
    "Apartment": "",
    "POBox": "null",
    "Description": ""
    },
    "Contact": {
    "Department": "",
    "PersonName": "aramex",
    "Title": "",
    "CompanyName": "aramex",
    "PhoneNumber1": "966512345678",
    "PhoneNumber1Ext": "",
    "PhoneNumber2": "",
    "PhoneNumber2Ext": "",
    "FaxNumber": "",
    "CellPhone": "966512345678",
    "EmailAddress": "test@test.com",
    "Type": ""
    }
    },
    "ThirdParty": {
    "Reference1": "",
    "Reference2": "",
    "AccountNumber": "71500004",
    "PartyAddress": {
    "Line1": "Test",
    "Line2": "",
    "Line3": "",
    "City": "riyadh",
    "StateOrProvinceCode": "3079",
    "PostCode": "",
    "CountryCode": "sa",
    "Longitude": 0,
    "Latitude": 0,
    "BuildingNumber": "null",
    "BuildingName": "null",
    "Floor": "null",
    "Apartment": "null",
    "POBox": "null",
    "Description": "null"
    },
    "Contact": {
    "Department": "",
    "PersonName": "test",
    "Title": "",
    "CompanyName": "test",
    "PhoneNumber1": "966512345678",
    "PhoneNumber1Ext": "",
    "PhoneNumber2": "",
    "PhoneNumber2Ext": "",
    "FaxNumber": "",
    "CellPhone": "966512345678",
    "EmailAddress": "",
    "Type": ""
    }
    },
    "ShippingDateTime": "\/Date(1708718162000)\/",
    "DueDate": "\/Date(1708718162000)\/",
    "Comments": "",
    "PickupLocation": "",
    "OperationsInstructions": "",
    "AccountingInstrcutions": "",
    "Details": {
    "Dimensions": "null",
    "ActualWeight": {
    "Unit": "KG",
    "Value": 0.5
    },
    "ChargeableWeight": "null",
    "DescriptionOfGoods": "Books",
    "GoodsOriginCountry": "sa",
    "NumberOfPieces": 1,
    "ProductGroup": "DOM",
    "ProductType": "CDS",
    "PaymentType": "P",
    "PaymentOptions": "",
    "CustomsValueAmount": "null",
    "CashOnDeliveryAmount": {
    "CurrencyCode": "SAR",
    "Value": 1.0
    },
    "InsuranceAmount": "null",
    "CashAdditionalAmount": "null",
    "CashAdditionalAmountDescription": "",
    "CollectAmount": "null",
    "Services": "CODS",
    "Items": []
    },
    "Attachments": [],
    "ForeignHAWB": "",
    "TransportType ": 0,
    "PickupGUID": "",
    "Number": "",
    "ScheduledDelivery": "null"
    }
    ],
    "Transaction": {
    "Reference1": "",
    "Reference2": "",
    "Reference3": "",
    "Reference4": "",
    "Reference5": ""
    }
    }

    response = requests.post(url, headers=headers, json=payload)

    if response.status_code == 200:
    # Request successful
    data = response.json()
    # Process the response data as needed
    print(data)
    else:
    # Request unsuccessful
    print(f"Request failed with status code {response.status_code}")
    print(response.text)



    This is the error i always get

    Request failed with status code 400
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Request Error</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
    </head>
    <body>
    <div id="content">
    <p class="heading1">Request Error</p>
    <p xmlns="">The server encountered an error processing the request. Please see the <a rel="help-page" href="https://ws.sbx.aramex.net/ShippingAPI.V2/Shipping/Service_1_0.svc/Json/help">service help page</a> for constructing valid requests to the service.
    The exception message is 'The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://ws.aramex.net/ShippingAPI/v1/:Shipments. The InnerException message was 'There was an error deserializing the object of type System.Collections.Generic.List`1[[Corp.ShippingAPI.FrontEnd.Contracts.Shipment, Corp.ShippingAPI.FrontEnd.Contracts, Version=1.359.0.0, Culture=neutral, PublicKeyToken=null]]. DateTime content '\/Date(1708718162000)\/' does not start with '\/Date(' and end with ')\/' as required for JSON.'. Please see InnerException for more details.'. See server logs for more details. The exception stack trace is: </p>
    <p> at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part)
    at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, Object[] parameters, PartInfo returnInfo, Object&amp; returnValue)
    at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBodyCore(XmlDictionaryReader reader, Object[] parameters, Boolean isRequest)
    at System.ServiceModel.Dispatcher.DataContractJsonSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)
    at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
    at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters)
    at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
    at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
    at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&amp; rpc)
    at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)
    at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>
    </div>
    </body>
    </html>
    Internal Server Error: /user/send_issue_policy/
    Traceback (most recent call last):
    File "/home/azzam/Desktop/mywebsite/env/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
    File "/home/azzam/Desktop/mywebsite/env/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/home/azzam/Desktop/mywebsite/env/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 56, in wrapper_view
    return view_func(*args, **kwargs)
    File "/home/azzam/Desktop/mywebsite/env/lib/python3.10/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
    File "/home/azzam/Desktop/mywebsite/env/lib/python3.10/site-packages/rest_framework/views.py", line 511, in dispatch
    self.response = self.finalize_response(request, response, *args, **kwargs)
    File "/home/azzam/Desktop/mywebsite/env/lib/python3.10/site-packages/rest_framework/views.py", line 423, in finalize_response
    assert isinstance(response, HttpResponseBase), (
    AssertionError: Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'NoneType'>`


    i want to send the request to aramex to create new lable i want to return the lable number so i can use it with print lable or tracking or give the user number of his lable The problem here is when I want to send the order to Aramex, a time-related problem occurs in "ShippingDateTime" i tried many times put none of them works.

    Continue reading...

Compartilhe esta Página