Getting a request error 422 upon get request - python

I have seen some posts about this but most of them used requests.post and I want a get request.
I am making a program that simply goes to a url and gets a list of orders.
This is the example response:
{
"page": 1,
"pageCount": 22,
"orderCount": 214,
"orders": [
{
"id": "c1497823-370c-4c7a-82cd-dacddb36fc30",
"productId": "1a641ba5-38df-4acb-86f7-f5c031e538a0",
"email": "demoemail#autobuy.io",
"ipAddress": "127.0.0.1",
"total": 18,
"currency": "USD",
"gateway": "Stripe",
"isComplete": true,
"createdAtUtc": "2019-10-15T01:44:10.5446599+00:00"
},
{
"id": "228f4ca4-5001-4c19-8350-f960f13d35a7",
"productId": "a0041cc0-2bc6-40a2-9084-5880bae5ecec",
"email": "demoemail#autobuy.io",
"ipAddress": "127.0.0.1",
"total": 50,
"currency": "USD",
"gateway": "Stripe",
"isComplete": true,
"createdAtUtc": "2019-10-15T01:43:17.8322919+00:00"
},
{
"id": "71aed9b2-4bd2-4a49-9e6a-82119e6e05bf",
"productId": "2e0ac75b-bfea-42f1-ad60-ead17825162a",
"email": "demoemail#autobuy.io",
"ipAddress": "127.0.0.1",
"total": 6,
"currency": "USD",
"gateway": "Stripe",
"isComplete": true,
"createdAtUtc": "2019-10-14T23:54:44.6217478+00:00"
}
]
}
This is what I get:
<Response [422]>
My code:
import requests
url = "https://autobuy.io/api/Orders?page=1"
headers = {
"Cache-Control": "no-cache",
"Pragma": "no-cache",
"APIKey": "<APIKEY>"
}
req = requests.get(url, headers=headers)
print(req)
Documentation for the api I'mtrying to use https://api.autobuy.io/?version=latest
edit: on their site it says
HEADERS
APIKey
2cdbdc48-b297-41ad-a234-329db0d2dbea
AutoBuy Api Key found in your shop settings
but then when I remove my Cache-Control and Pragma headers I get an error that has to do with my headers being cached (cause the site is behind a CDN?)

Ended up being a bug in the site, my dashboard was showing an invalid key.

Related

Python PUT requests to nexus repo

I have a nexus, yum proxy repo which has some components as below
"name": "internal-test",
"online": true,
"storage": {
"blobStoreName": "default",
"strictContentTypeValidation": true
},
"cleanup": {
"policyNames": [
"string"
]
},
"proxy": {
"remoteUrl": "https://remote.repository.com",
"contentMaxAge": 1440,
"metadataMaxAge": 1440
},
"negativeCache": {
"enabled": true,
"timeToLive": 1440
},
"httpClient": {
"blocked": false,
"autoBlock": true,
"connection": {
"retries": 0,
"userAgentSuffix": "string",
"timeout": 60,
"enableCircularRedirects": false,
"enableCookies": false,
"useTrustStore": false
},
"authentication": {
"type": "username",
"username": "string",
"password": "string",
"ntlmHost": "string",
"ntlmDomain": "string"
}
},
"routingRule": "string",
"yumSigning": {
"keypair": "string",
"passphrase": "string"
}
}'
I am trying to use requests in python and used below code to modify "metadataMaxAge": 1440 parameter in nexus repo (as shown above) to this metadataMaxAge": -1.
url = "https://***/"
auth1 = ('user', 'pasword')
payload = {'metadataMaxAge': '-1'}
res = requests.put(url, json=payload, auth=auth1, verify=False)
But I get the response 400, so can anyone help me out here on how to fix this ?

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

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?

Extracting a specific key from JSON

I'm working with the requests library, Python 3.6, and an SMS platform's Swagger API.
I'm a little bit confused as to how I would extract a specific key:value from this JSON hierarchy.
Here is the URL to the API documentation:
api docs
I am trying to query the endpoint for a specific phone number based on an ID.
This is the URL to query: https://api.kenect.com/v1/conversations/****ID***
Here is the JSON hierarchy, so I would like to use the 'phoneNumbers' key and the 'number' value:
{
"archiveDate": 1514801105185,
"assignedTeamId": 0,
"assignedTeamName": "string",
"assignedUser": {
"departmentId": 1234,
"firstName": "John",
"id": 1234,
"inactiveDate": 1514801105185,
"includeSignature": true,
"lastName": "Doe",
"messageSignature": "Best, John Doe",
"username": "jdoe#mymail.com"
},
"contact": {
"createdDate": 1514801105185,
"emailAddress": "contact#email.com",
"externalId": "8adf-7865",
"firstActiveDate": 1514801105185,
"firstName": "Jane",
"groups": [
{
"createdDate": 1514801105185,
"id": 1234,
"locationId": 1234,
"name": "the inner circle",
"updatedDate": 1514801105185
}
],
"id": 1234,
"inactiveDate": 1514801105185,
"lastContacted": 1514801105185,
"lastName": "Doe",
"locationId": 12345,
"note": "string",
"phoneNumbers": [
{
"city": "Pleasant Grove",
"country": "US",
"createdDate": 1514801105185,
"id": 1234,
"messageOptInDate": 1514801105185,
"messageStopDate": 1514801105185,
"number": "+18015552671",
"primary": true,
"sentOptInMessageDate": 1514801105185,
"smsCapable": true,
"state": "UT",
"status": true,
"type": "CELL",
"updatedDate": 1514801105185,
"zipCode": 84062
}
],
"reviewRequestSent": 1514801105185,
"source": "LIGHTSPEED",
"updatedDate": 1514801105185
},
"createdDate": 1514801105185,
"dataAssigned": 0,
"id": 1234,
"lastMessageType": "FACEBOOK",
"lastSent": 1514801105185,
"locationId": 0,
"messages": [
{
"assignedUserId": 0,
"attachments": [
{
"contentType": "string",
"createdDate": 1514801105185,
"friendlyName": "string",
"id": 1234,
"md5": "string",
"size": 16384,
"thumbnailBase64": "string",
"updatedDate": 1514801105185
}
],
"body": "string",
"contactId": 0,
"conversationId": 0,
"error": "string",
"id": 1234,
"kind": "string",
"locationId": 0,
"outgoing": true,
"readDate": 0,
"respondedDate": 0,
"reviewRequest": true,
"sentDate": 0,
"status": "string",
"type": "FACEBOOK"
}
],
"newConversation": true,
"read": true,
"responded": true,
"snippet": "string",
"subject": "string",
"updatedDate": 1514801105185
}
And finally, here is the code I am trying. I had used a similar function to obtain the message body from this same endpoint, but it's not working for the phone number.
url='https://api.kenect.com/v1/conversations/'
h = {'accept': recContentType,
'content-type': recContentType,
'x-api-token': apiToken,
'x-api-Key': apiKey}
p={'id': id}
response=requests.get(url + id, headers=h, params=p).json()
for item in response['phoneNumber']:
pnum = item['number']
print(pnum)
What am I doing incorrectly here?
Here's the path you'll need to access to get to number:
.contact.phoneNumbers[0].number
So, you'll need this:
number = response['contact']['phoneNumbers'][0]['number']
print(number)
Or editing your code:
url='https://api.kenect.com/v1/conversations/'
h = {'accept': recContentType,
'content-type': recContentType,
'x-api-token': apiToken,
'x-api-Key': apiKey}
p={'id': id}
response=requests.get(url + id, headers=h, params=p).json()
for item in response['contact']['phoneNumbers']:
pnum = item['number']
print(pnum)
As per the comments below, the code above can result in errors if a field in the json is missing. For instance, if a reponse doesn't have a contact there will be an issue.
Consider adding some more bullet proofing to the code above as follows:
url='https://api.kenect.com/v1/conversations/'
h = {'accept': recContentType,
'content-type': recContentType,
'x-api-token': apiToken,
'x-api-Key': apiKey}
p={'id': id}
response=requests.get(url + id, headers=h, params=p).json()
if response:
if 'contact' in response:
if 'phoneNumbers' in response['contact']:
for item in response['contact']['phoneNumbers']:
pnum = item['number']
print(pnum)
I think there is enough checking in the new version of code to avoid the error you described in the comments below. Now, we only process the response when it is non empty and when it contains a contact key which in turn has to contain the phoneNumbers key. There is still an opportunity to print None, when a phoneNumber doesn't contain a number, but that won't generate an error. Just be aware that pnum could be None.

PayPal Webhook request format

Any chance someone could help me understand how I could handle paypal webhooks better? I believe the webhook sends a POST request to the chosen URL, but how is the information encoded? Is it a raw JSON? form?
The webhook sent to the chosen URL from PayPal is in JSON form.
https://developer.paypal.com/docs/api/webhooks/v1/#simulate-event
{
"id": "8PT597110X687430LKGECATA",
"create_time": "2013-06-25T21:41:28Z",
"resource_type": "authorization",
"event_version": "1.0",
"event_type": "PAYMENT.AUTHORIZATION.CREATED",
"summary": "A payment authorization was created",
"resource_version": "1.0",
"resource": {
"id": "2DC87612EK520411B",
"create_time": "2013-06-25T21:39:15Z",
"update_time": "2013-06-25T21:39:17Z",
"state": "authorized",
"amount": {
"total": "7.47",
"currency": "USD",
"details": {
"subtotal": "7.47"
}
},
"parent_payment": "PAY-36246664YD343335CKHFA4AY",
"valid_until": "2013-07-24T21:39:15Z",
"links": [
{
"href": "https://api.paypal.com/v1/payments/authorization/2DC87612EK520411B",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.paypal.com/v1/payments/authorization/2DC87612EK520411B/capture",
"rel": "capture",
"method": "POST"
},
{
"href": "https://api.paypal.com/v1/payments/authorization/2DC87612EK520411B/void",
"rel": "void",
"method": "POST"
},
{
"href": "https://api.paypal.com/v1/payments/payment/PAY-36246664YD343335CKHFA4AY",
"rel": "parent_payment",
"method": "GET"
}
]
},
"links": [
{
"href": "https://api.paypal.com/v1/notfications/webhooks-events/8PT597110X687430LKGECATA",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.paypal.com/v1/notfications/webhooks-events/8PT597110X687430LKGECATA/resend",
"rel": "resend",
"method": "POST"
}
]
}
Try
file_get_contents('php://input')
instead of reading the $_POST variable. I do not know why Paypal sends the $_POST request in a way where reading it through $_POST would be possible, but the above way works for me.

Website always hangs using python requests library

I am trying to use the python requests library to get the html from this url https://www.adidas.com/api/products/EF2302/availability?sitePath=us
However every time I run my code it hangs when making the get request
header = BASE_REQUEST_HEADER
url = 'https://www.adidas.com/api/products/EF2302/availability?sitePath=us'
r = requests.get(url, headers = header)
I checked the network tab in chrome and copied all the headers used including user agent so that is not the issue. I was also able to load the page in chrome with javascript and cookies disabled.
This code works fine with other websites. I simply cant get a response from any of the adidas websites (including https://www.adidas.com/us).
Any suggestions are greatly appreciated.
This site doesn't like the default User-Agent field supplied by requests, change it to Firefox/Chrome (I chose Firefox in my example), and you can read data successfully:
from bs4 import BeautifulSoup
import requests
import json
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0'}
url = 'https://www.adidas.com/api/products/EF2302/availability?sitePath=us'
r = requests.get(url, headers=headers)
json_data = json.loads(r.text)
print(json.dumps(json_data, indent=4))
Prints:
{
"id": "EF2302",
"availability_status": "PREORDER",
"variation_list": [
{
"sku": "EF2302_530",
"availability": 15,
"availability_status": "PREORDER",
"size": "4",
"instock_date": "2018-08-16T00:00:00.000Z"
},
{
"sku": "EF2302_550",
"availability": 15,
"availability_status": "PREORDER",
"size": "5",
"instock_date": "2018-08-16T00:00:00.000Z"
},
{
"sku": "EF2302_570",
"availability": 15,
"availability_status": "PREORDER",
"size": "6",
"instock_date": "2018-08-16T00:00:00.000Z"
},
{
"sku": "EF2302_590",
"availability": 15,
"availability_status": "PREORDER",
"size": "7",
"instock_date": "2018-08-16T00:00:00.000Z"
},
{
"sku": "EF2302_610",
"availability": 15,
"availability_status": "PREORDER",
"size": "8",
"instock_date": "2018-08-16T00:00:00.000Z"
},
{
"sku": "EF2302_630",
"availability": 15,
"availability_status": "PREORDER",
"size": "9",
"instock_date": "2018-08-16T00:00:00.000Z"
},
{
"sku": "EF2302_650",
"availability": 15,
"availability_status": "PREORDER",
"size": "10",
"instock_date": "2018-08-16T00:00:00.000Z"
},
{
"sku": "EF2302_670",
"availability": 15,
"availability_status": "PREORDER",
"size": "11",
"instock_date": "2018-08-16T00:00:00.000Z"
},
{
"sku": "EF2302_690",
"availability": 15,
"availability_status": "PREORDER",
"size": "12",
"instock_date": "2018-08-16T00:00:00.000Z"
},
{
"sku": "EF2302_710",
"availability": 15,
"availability_status": "PREORDER",
"size": "13",
"instock_date": "2018-08-16T00:00:00.000Z"
}
]
}
One different is the User-agent field, which requests sets as
User-Agent: python-requests/2.18.4
Adidas may be just dropping these http requests to stop people abusing their system.
(btw, it also happens for just www.adidas.com)
I reproduced the issue and took a look at wireshark packet sniffer. It seems the http request is good and there is tcp acknowledgement but no http reply.

Categories