2

I'm trying to validade an address in FedEX API using Python 3.8 and it returns an error of invalid field value

First I connect to the Auth API

payload={"grant_type": "client_credentials",'client_id':Client_id,'client_secret':Client_secret}
url = "https://apis-sandbox.fedex.com/oauth/token"
headers = {'Content-Type': "application/x-www-form-urlencoded"}
response=requests.post(url, data=(payload), headers=headers)

And it returns a message with the Auth token correctly

{"access_token":"eyJhbGciOiJSUzI1NiIsInRM5U0F2eUs1ZVFBVTFzS5k","token_type":"bearer","expires_in":3599,"scope":"CXS SECURE"}

Then I just get the token to use it in next transactions

token = json.loads(response.text)['access_token']

Then I prepare the next payload for address validation API

payload_valid_address = {
    "addressesToValidate": [
        {
    "address":
            {
            "streetLines": ["7372 PARKRIDGE BLVD"],
            "city": "Irving",
            "stateOrProvinceCode": "TX",
            "postalCode": "75063-8659",
            "countryCode": "US"
            }
        }
    ]
}

And send the request to the new endpoint with the given token

url = "https://apis-sandbox.fedex.com/address/v1/addresses/resolve"
headers = {
    'Content-Type': "application/json",
    'X-locale': "en_US",
    'Authorization': 'Bearer '+ token
    }

response = requests.post(url, data=payload_valid_address, headers=headers)

print(response.text)

and get the error

<Response [422]>
{"transactionId":"50eae03e-0fec-4ec7-b068-d5c456b64fe5","errors":[{"code":"INVALID.INPUT.EXCEPTION","message":"Invalid field value in the input"}]}

I have made inumerous tests and I don't get the invalid field. Anyone know what is happening and can help?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Flavio Amadio
  • 35
  • 1
  • 6
  • 1
    FedEX uses OAuth 2.0 token authentication method to authorize the application and authenticate API requests. – Flavio Amadio Feb 22 '22 at 22:41
  • 1
    Try using `string` object from `json` by: ```import json payload_valid_address = '''{ "addressesToValidate": [ { "address": { "streetLines": ["7372 PARKRIDGE BLVD"], "city": "Irving", "stateOrProvinceCode": "TX", "postalCode": "75063-8659", "countryCode": "US" } } ] }''' ``` PAYLOAD_VALID_ADDRESS = json.load(PAYLOAD_VALID_ADDRESS) – user11717481 Feb 22 '22 at 22:50
  • 1
    Same issue. I tried to use other endpoit API's with other payloads and I get the same issue. Other point is that the error 422 is not listed in FedEX documents and its Json Schema. I think the problem is not the payload but in the way that I'm accessing the API's or validating the token. – Flavio Amadio Feb 23 '22 at 11:34
  • @FlavioAmadio Yeah, that's correct. Error 422 is not listed nor documented in Fedex. That's exaxctly what I'm getting on an a PHP implementation I'm doing now. Still trying to figure out what's going on. – MarkSkayff Mar 01 '23 at 16:59

4 Answers4

2

payload = json.dumps({input payload}) this works to prevent the response error 422 <Response [422]>

Umk
  • 71
  • 5
1

I have fixed it

For any reason, convert the string payload_valid_address to Json in 2 steps doesn't work

payload_valid_address = {....}
payload = json.dumps(payload_valid_address)

But if made in just one step it pass the API request

payload_valid_address = json.dumps({....})
Flavio Amadio
  • 35
  • 1
  • 6
1

I also had this error and additionally had to use json.loads(json_string) when the json data contained key words like false, true or null.

import json

json_string = r'''{
  "accountNumber": {
    "value": "802255209"
  },
  "requestedShipment": {
    "shipper": {
      "address": {
        "postalCode": 75063,
        "countryCode": "US"
      }
    },
    "recipient": {
      "address": {
        "postalCode": "m1m1m1",
        "countryCode": "CA"
      }
    },
    "shipDateStamp": "2020-07-03",
    "pickupType": "DROPOFF_AT_FEDEX_LOCATION",
    "serviceType": "INTERNATIONAL_PRIORITY",
    "rateRequestType": [
      "LIST",
      "ACCOUNT"
    ],
    "customsClearanceDetail": {
      "dutiesPayment": {
        "paymentType": "SENDER",
        "payor": {
          "responsibleParty": null
        }
      },
      "commodities": [
        {
          "description": "Camera",
          "quantity": 1,
          "quantityUnits": "PCS",
          "weight": {
            "units": "KG",
            "value": 20
          },
          "customsValue": {
            "amount": 100,
            "currency": "USD"
          }
        }
      ]
    },
    "requestedPackageLineItems": [
      {
        "weight": {
          "units": "KG",
          "value": 20
        }
      }
    ]
  }
}'''

data = json.loads(json_string)
data
response = requests.request("POST", url, data=json.dumps(data), headers=headers)
Martin H
  • 155
  • 2
  • 3
  • 14
0

I have managed to get Fedex to accept my ayload, however i get this message:

"code":"INVALID.INPUT.EXCEPTION","message":"Validation failed for object='shipShipmentInputVO'. Error count: 1","parameterList":[{"key":"NotNull.shipShipmentInputVO.labelResponseOptions","value":"labelResponseOptions cannot be null"

and when I add the required parameter

'labelResponseOptions': "URL_ONLY"

I get this:

"code":"SYSTEM.UNEXPECTED.ERROR","message":"The system has experienced an unexpected problem and is unable to complete your request.  Please try again later.  We regret any inconvenience."

Is this really some Fedex internal error?

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32817848) – Andrew Ryan Oct 03 '22 at 03:18