1

I am trying to get some query results using bulk api from Python. I have been able to get the token but I run into an error when I try to create the batch job based on documentation but I run into error

Status : 400
Response :  [{"errorCode":"API_ERROR","message":"Value does not match expected type."}]

Python Code :

def create_bulk_job(endpoint, method, body):
    authResponse = authenticate() # Makes the authentication request
    access_token = authResponse.json().get("access_token")
    instance_url = authResponse.json().get("instance_url")
    headers = {
        'Content-type': 'application/json',
        'Accept-Encoding': 'gzip',
        'Authorization': 'Bearer %s' % access_token,
        'Accept': 'application/json'
    }
    print('Resource : ', instance_url+endpoint)
    print('Body : ',body)
    start_response = requests.post(instance_url+endpoint,headers=headers, json=body)
    print('Status :',start_response.status_code)
    print('Response : ',start_response.text)
    print('URL  :',start_response.request.url)    
    print('Headers :', start_response.request.headers)
    print("Body :", start_response.request.body)

# To make the request
start_end_point = "/services/data/v51.0/jobs/query"
    start_body = {
        "operation": "query",
        "query":"SELECT Id, Name FROM Account"
    }
    create_bulk_job(start_end_point,"POST",json.dumps(start_body))

The above code gives me the request as :

Headers : {'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip', 'Accept': 'application/json', 'Connection': 'keep-alive', 'Content-type': 'application/json', 'Authorization': 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'Content-Length': '73'}
Body : b'"{\\"operation\\": \\"query\\", \\"query\\": \\"SELECT Id, Name FROM Account\\"}"'

The same request made in postman appears as:

POST /services/data/v51.0/jobs/query HTTP/1.1
Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Content-Type: application/json
User-Agent: PostmanRuntime/7.26.10
Accept: */*
Postman-Token: b15efa22-198c-4cc2-a500-363b38a86840
Host: xxxxxxxxxxxx.my.salesforce.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 69
Cookie: BrowserId=YM41u6wGEeutgk2T5kSWmw
{
"operation": "query",
"query": "SELECT Id, Name FROM Account"
}

I can't understand what am I missing out when building the request. I have confirmed that the endpoints are the same.

3
  • My advice would be to use some service in order to inspect web requests (like requestbin.net) to track what is an actual payload/headers received from postman/python and compare those
    – kurunve
    Commented May 3, 2021 at 12:27
  • @kurunve i tried doing using pipedream that and the only difference i observe is in the body : Postman :"body": { "operation": "query", "query": "SELECT Id, Name FROM Account" } and python "body": "{\"operation\": \"query\", \"query\": \"SELECT Id, Name FROM Account\"}", Not sure why this could be happening
    – N_H1922
    Commented May 3, 2021 at 12:43
  • 1
    @kurunve thank you ! i understood that the python function takes the native dictionary as a parameter and not the string generated by JSON.dumps(). Sending in the actual dictionary as request body made it work.
    – N_H1922
    Commented May 3, 2021 at 12:46

0

You must log in to answer this question.

Browse other questions tagged .