SOLVED

Invalid Content-Type on Bulk API call

Go to solution
Phillip_Wild
Level 10 - Community Advisor

Invalid Content-Type on Bulk API call

Hi everyone

Having some trouble with a Marketo Bulk API job.  Here's the Python code:

 

# marketo params
parameters = {
    'format': 'csv',
    'access_token': access_token,
    'createdAt': '{"startAt": "2020-02-15T23:59:59-00:00","endAt": "2020-02-26T23:59:59-00:00"}',
    'activityTypeIds': '[113]'
}
headers = {'accept': 'application/json'}
# first call
response = requests.post(
    "https://(redacted).mktorest.com/bulk/v1/activities/export/create.json",
    params=parameters,
    headers=headers
)

 

The error I get is 612 - invalid content type. This is a little confusing to me though. I'm not uploading a multipart file or anything, so shouldn't it be regular application/json?

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Invalid Content-Type on Bulk API call

Yes, when you POST JSON, the request Content-Type should be application/json.

 

So make sure you've set that header! Or use json=payload, which sets it automatically.  

 

Note the way you're mixing params and body is pretty confusing, even if some Marketo API endpoints accept this mixture it's not advisable.

View solution in original post

8 REPLIES 8
SanfordWhiteman
Level 10 - Community Moderator

Re: Invalid Content-Type on Bulk API call

Yes, when you POST JSON, the request Content-Type should be application/json.

 

So make sure you've set that header! Or use json=payload, which sets it automatically.  

 

Note the way you're mixing params and body is pretty confusing, even if some Marketo API endpoints accept this mixture it's not advisable.

Phillip_Wild
Level 10 - Community Advisor

Re: Invalid Content-Type on Bulk API call

Weird. Just tried in Postman, with these details:

 

https://(redacted).mktorest.com/bulk/v1/activities/export/create.json?format=csv&access_token=(redacted)&createdAt={ "startAt": "2020-02-15T23:59:59-00:00","endAt": "2020-02-26T23:59:59-00:00"}&activityTypeIds=[113]

 

Header is: 

 

Content-Type: application/json

 

And error code is:

 

 

{
    "requestId": "8c00#1709e393153",
    "success": false,
    "errors": [
        {
            "code": "609",
            "message": "Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@9bfa1393"
        }
    ]
}

 

 

Hmmm what am I missing here?

 

Also, could you elaborate on what you mean by "mixing params and body" in the Python code? 

SanfordWhiteman
Level 10 - Community Moderator

Re: Invalid Content-Type on Bulk API call


Also, could you elaborate on what you mean by "mixing params and body" 


You're passing some parameters in the query string (though apparently not URL-encoding them, which is bound to fail with some values!) yet POSTing other parameters in the JSON body.

 

You should be putting everything in the body (a.k.a. payload, a.k.a. request body, a.k.a. entity-body). The fact that certain things seem to work when passed in the query string is coincidental. (It's a factor of the way some web servers have a combined "params" object that merges query params, post bodies, HTTP headers, even environment variables and cookies - but isn't supported.)

Phillip_Wild
Level 10 - Community Advisor

Re: Invalid Content-Type on Bulk API call

Ah. But given my initial code was in Python, does't the requests library take care of that? In Postman I see what you mean though, but that was my secondary test. 

 

So much left to learn!

SanfordWhiteman
Level 10 - Community Moderator

Re: Invalid Content-Type on Bulk API call

Requests doesn't automatically take care of it.

 

You still need to pass the right stuff in the body, and set the Content-Type header if you don't expressly use the named json method.

 


 

N.B. Almost all HTTP client libraries are like this,. There may be a shortcut method for serializing an object to JSON, and if you use that client method -- one that has "json" in the name -- chances are 99.999% that it'll set the related Content-Type for you.

 

But there will also be a way to craft your own payload, because you'll need that customizability at times. And when you use that "plain HTTP" method, then the headers are not going to automatically synchronize with the text you happened to use as the payload.  After all it is just text at that point!

 

Take the Fetch API for example. Even though JS is as JSON-friendly as you can get, the default type of a String body is text/plain. Not application/json. Even if you JSON.stringify()-ed an object to get the String, it's text as far as Fetch knows.

vaibhvpatil123
Level 1

Re: Invalid Content-Type on Bulk API call

Bulk import fails with 

curl --location --request POST 'https://asasas.mktorest.com/rest/v1/leads.json?json=payload' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer asasasas:sj' \
--form 'file=@"/Users/asasas/Downloads/All_People.json"'
<center>
<h1>400 Bad Request</h1>
</center>
<hr>
<center>nginx</center>
</body>
 
 
DanBrum
Level 1

Re: Invalid Content-Type on Bulk API call

The body literally has to be a JSON string -- just getting into using the API myself.  Please look at the docs here:
https://developers.marketo.com/rest-api/bulk-extract/bulk-activity-extract/

 

And the request needs to be submitted as a POST, not GET.  This literally worked for me in Postman:

body

----------

{
"format": "CSV",
"filter": {
"createdAt": {
"startAt": "2020-02-01T00:00:00Z",
"endAt": "2020-03-01T00:00:00Z"
},
"acitivyTypeIds": [
113
]
}
}

 

response
-----------

{
    "requestId""13b9#170a17da0c4",
    "result": [
        {
            "exportId""ae60fc08-cd1b-46ed-8905-c84b858b1c66",
            "format""CSV",
            "status""Created",
            "createdAt""2020-03-03T17:42:09Z"
        }
    ],
    "success"true
}
 
Good luck!
SanfordWhiteman
Level 10 - Community Moderator

Re: Invalid Content-Type on Bulk API call

Correct, that's what I said above. Pls highlight your JSON though.