API FedEX "INVALID.INPUT.EXCEPTION","message":"Invalid field value in the input" - python

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?

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

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({....})

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)

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?

Related

How to request GET method in specific url using Postman?

I want to get some results using Postman.
expected result :
{
"status": "200",
"message": "success",
"results": {
"count": "6816",
"data": [
{
"timestamp": "2022-07-17T10:47:03Z",
"sender": "0x2F664960a7FBdaDA8e133DbAF1Bfc27DCCBADBfb",
"receiver": "0xA68A135Ccd37E720000fC30CFcc453b15F8040df",
"value": "0",
"status": "1",
"message": "",
"transaction_hash": "0x1a03528eecf165678d7fbcb9ceeee45a98b02f10a5ce30236ef29ee5b3747c84",
"block_number": "2472",
"contract_address": null,
"trace_address": [
"0",
"1",
"1"
],
"trace_type": "staticcall",
"sub_traces": null,
"transaction_index": "0",
"gas_used": "2715",
"gas_limit": "29039032",
"external_receiver": "0",
"input": "0x0d2020dd476f7665726e616e6365436f6e74726163740000000000000000000000000000"
},
...
]
}
}
When I request GET Method for example,
API Docs : https://docs.wemix.com/v/en/dapp-developer/api-reference/account-apis#get-internal-transactions-by-address-and-get-internal-transactions-by-transaction-hash
GET https://explorerapi.test.wemix.com/v1/accounts/0xA68A135Ccd37E720000fC30CFcc453b15F8040df/internal-transactions
api-key: 1ba5e446edf1997f67b51bf9e60b3fbba6fa1bf84301115292805d7e24f43539RESPONSE DATA
I requests GET method using 'api-key' using Postman
but, my response is,
{
"status": "401",
"message": "Unauthorized"
}
How to fix it to get proper response?
Or, is there a good way to call API with Python?
In addition to Khalil's answer - to do the same in python you could do the following:
import requests
headers = {'api-key': '1ba5e446edf1997f67b51bf9e60b3fbba6fa1bf84301115292805d7e24f43539'}
response = requests.get('https://explorerapi.test.wemix.com/v1/accounts/0xA68A135Ccd37E720000fC30CFcc453b15F8040df/internal-transactions', headers=headers)
print(response)
<Response [200]> which will get you the data that you desire.
The API Key that was provided had RESPONSE DATA at the end which was invalid as the API key.
On the Headers tab, you need to add a key api-key with the api key 1ba5e446edf1997f67b51bf9e60b3fbba6fa1bf84301115292805d7e24f43539 in the value.

Conditional variable in payload

I am a very beginner and struggling with a POST Request.
I would like to have a conditional variable in payload which skips project services if they are not active in the project. To have a list of active services I called GET Projects and exported the list of projects to csv where projects names are rows and services are columns. Whenever service is active it has value 1 in its column. Is it possible to tailor services to payload if value in the csv is !=0?
start = "https://developer.api.autodesk.com/bim360/admin/v1/projects/"
end = "/users"
for i in range (len(projects_df)):
payload = json.dumps({
"email": "dummy#lalaland.com",
"companyId": "0000",
"roleIds": [
"0000"
],
"services": [
{
"serviceName": "documentManagement",
"access": "administrator"
},
{
"serviceName": "projectAdministration",
"access": "administrator"
},
{
"serviceName": "projectManagement",
"access": "administrator"
},
{
"serviceName": "costManagement",
"access": "administrator"
},
{
"serviceName": "fieldManagement",
"access": "administrator"
},
{
"serviceName": "modelCoordination",
"access": "administrator"
},
{
"serviceName": "insight",
"access": "administrator"
}
]
})
headers = {
'Authorization': 'Bearer ' + access_token_beta,
'Content-Type': 'application/json',
'User-Id': 'XXXXX'
}
project_id = projects_df.iloc[i,0]
url = start+project_id+end
response = requests.request("POST", url, headers=headers, data=payload)
jsonAsPythonValue = json.loads(response.text)
if (i == 0):
post_user = pd.DataFrame.from_dict(pd.json_normalize(jsonAsPythonValue))
else:
post_user = post_user.append(pd.DataFrame.from_dict(pd.json_normalize(jsonAsPythonValue)),ignore_index=True)
i=i+1
Thank you in advance!
What I usually do is create payload with a minimal set of data, and then have a series of if conditions that add to it as necessary.
payload = {
'key1': 'value1',
'key2': 'value2'
}
if condition1:
payload['key3'] = 'value3'
if condition2:
payload['key4'] = 'value4'

Can you get a more comprehensive http response using python requests?

I tried calling an API using both python and nodejs but the format in which the response is in is quite different.
For nodejs, it returns the statuscode, headers, body, request etc.
E.g.
(Only an example, not the actual response so you can ignore any syntax errors)
{
"statusCode" : 200,
"headers" : {
'xxxx' : 'xxxx"
},
"body" : {
"name" : "james",
"age" : 35
},
"request" {
"method" : "POST"
}
}
For python, it only returns the response body.
E.g.
(Only an example, not the actual response so you can ignore any syntax errors)
{
'name' : james,
'age' : 35
}
I am aware that i am able to get the headers etc using python request response object such as response.headers etc but is there a way that allows me to a similar response like nodejs. I know about response.dir but i need to forward this response so the format of response.dir is not accepted.
Thanks all!
You can compile your own response, similar to what you get from nodejs, something like
import json
import requests
url = 'http://date.jsontest.com/'
r = requests.get(url)
j = {
"statusCode": r.status_code,
"headers": dict(r.headers),
"body": r.json(),
"request": {
"method": r.request.method
}
}
print(json.dumps(j, indent=4))
Will produce:
{
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
"X-Cloud-Trace-Context": "9b4e313aa5e6373ddc997dc963ee8f1b",
"Date": "Thu, 23 Jun 2022 03:11:27 GMT",
"Server": "Google Frontend",
"Content-Length": "100"
},
"body": {
"date": "06-23-2022",
"milliseconds_since_epoch": 1655953887719,
"time": "03:11:27 AM"
},
"request": {
"method": "GET"
}
}

Extract data from API call and save file

after making post call of API I want to extract specific key/value and than save it onto a text file.
What have been done so far:-
(1)Rest api call and return list
import requests
import json
#API details
url = "http://192.168.1.100:9792/api/scan"
body = json.dumps({"service":"scan", "user_id":"1", "action":"read_all", "code":"0"})
headers = {'Content-Type': 'application/json'}
#Making http post request
response = requests.post(url, headers=headers, data=body, verify=False)
#Decode response.json() method to a python dictionary for data process utilization
dictData = response.json()
#Json string
json_str = json.dumps(dictData)
print(json_str)
print(json_str) output as below
{
"node": [
{
"id": "123",
"ip": "10.101.10.1",
"model": "md1",
"type": "basic",
"name": "aaa"
},
{
"id": "456",
"ip": "10.101.10.2",
"model": "sp3",
"type": "extra",
"name": "bbb"
},
{
"id": "789",
"ip": "1.1.1.1",
"model": "md1",
"type": "basic",
"name": "ccc"
},
{
"id": "101",
"ip": "2.2.2.2",
"model": "advance",
"type": "sw1",
"name": "ddd"
}
],
"status": "success"
}
(2)Extract specific key/value, This is where I'm getting error to get the key/value from the list
for i in json_str["node"]:
if i["type"]=="basic" or i["type"]=="sw1" :
print(i["name"],i["ip"], i["type"])
I'm getting error
for i in json_str["node"]:
TypeError: string indices must be integers, not str
I tried change to json_str[0] but it still doesn't return the key/value that i want.
Please assist further. thanks
Just use back dictData as it already in dictionary
for i in dictData["node"]
You dumped the json into str
Again to work with dict first load the json and then try
json_str = json.dumps(dictData)
json_dict = json.loads(json_str)
print(json_dict)

Paypal Rest API keeps returning malformed json error

I am trying to create payment using paypal Rest API, but I keep getting this error response:
{"name":"MALFORMED_REQUEST","message":"The request JSON is not well formed.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"262cbdc417df7"}
here with my code:
payment_url = 'https://api.sandbox.paypal.com/v1/payments/payment'
headers = {"Content-Type": "application/json", "Authorization": "Bearer %s" % access_token}
data = {
"intent": "sale",
"redirect_urls": {
"return_url": "http://localhost:8080/index.html",
"cancel_url": "http://localhost:8080/index.html"
},
"payer": {
"payment_method": "paypal"
},
"transactions": [
{
"amount": {
"total": "7.47",
"currency": "USD"
},
"details": {
"subtotal": "7.41",
"tax": "0.03",
"shipping": "0.03"
},
"description": "This is the payment transaction description.",
"item_list": {
"items": [
{
"quantity": "1",
"name": "item",
"price": "7.41",
"currency": "USD",
"sku": "item"
}]
}
}
]
}
print headers
print data
r = requests.post(payment_url, headers=headers, data=data)
print 'payment res', r.text
And I only get the response like this:
{"name":"MALFORMED_REQUEST","message":"The request JSON is not well formed.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"262cbdc417df7"}
I have seen quite a few questions regarding this kind of error, but none of them have solution yet. :(
The json formated post data is obviously valid. Otherwise, requests post method would raise exceptions. And the response is returned from paypal server, but I can't find any information from the link it gave. I have checked Rest API documentation, and I think I made the request exactly as the samples. What did I miss?
Any kind advice or solution would be appreciated.
Your return_url and cancel_url values need to have quotes around them. As the message says, your JSON is malformed.
Try this - http://jsonlint.com/ to see your errors.

Categories