Graph API: JSON batching to upload item to OneDrive is failing - json

I am trying to upload a text file to OneDrive using Graph APIs and I also want to update it in the same request using JSON batch.
My JSON request body is below:
{
"requests":[
{
"id":"1",
"method":"PUT",
"url":"/drives/b!ddubdQaackeT9nu3x4onivgPxHH2-
gpFsk_mo9hryZabqK7w279YSpMqiNodZDaa/items/01BTTSDZ56Y2GOVW772
5BZO354PWSELRRZ:/abc2.txt:/content",
"headers":{
"Content-Type":"application/octet-stream",
"Content-Length":"21"
},
"body":{
"content":"Test content for body"
}
},
{
"id":"2",
"method":"PATCH",
"url":"/drives/b!ddubdQaackeT9nu3x4onivgPxHH2-
gpFsk_mo9hryZabqK7w279YSpMqiNodZDaa/items/01BTTSDZ56Y
2GOVW7725BZO354PWSELRRZ:/abc2.txt",
"headers":{
"Content-Type":"application/json; charset=utf-8"
},
"body":{
"fileSystemInfo":{
"lastModifiedDateTime":"2020-08-09T00:49:37.7758742+03:00"
}
},
"dependsOn":["1"]
}
]
}
When I send this request from my code, I always get a response "Invalid body for request id: 1. The body must be a valid base64 string or JSON.".
Postman refused to run the above request with the message "Method not allowed".
In the above example, I am uploading and updating text files but my code will have to handle all file types (e.g. images, videos, etc.)
Not sure if I am correctly specifying all the JSON fields. Unfortunately unable to find much info on this. Any help would be appreciated.

Related

MS Graph batching create list item (SPO) Invalid batch payload format

I did see this same error, in c# has an open question, but I'm using PowerShell and using POST rather than PATCH so I've opened a separate question.
I'm having an issue when using JSON batching, particularly that when I include the header for Content-Type, I receive:
Invoke-RestMethod : {
"error": {
"code": "BadRequest",
"message": "Invalid batch payload format.",
"innerError": {
"date": "2020-10-14T00:25:46",
"request-id": "aa535dbb-efe8-450e-911d-143554ed9027",
"client-request-id": "aa535dbb-efe8-450e-911d-143554ed9027"
}
}
}
I first had missed including the headers at all, and was receiving the error:
{
"error": {
"code": "BadRequest",
"message": "Write request id : 2 does not contain Content-Type header or body.",
"innerError": {
"date": "2020-10-14T00:46:58",
"request-id": "3601be6d-a861-4947-936b-451cd9de80c3",
"client-request-id": "3601be6d-a861-4947-936b-451cd9de80c3"
}
}
}
The body of my HTTP request to https://graph.microsoft.com/v1.0/$batch is an array of PSCustomObjects that look like this:
id : 1
method : POST
url : sites/8c3cb1ef-4116-b0e4-6d0b-25d0f333a4ed/lists/a2b2d34e-6d32-df22-d562-472d3d8385d2/items
body : {
"fields": {
"DisplayName": "user1#contoso.com",
"CreatedDateTime": "2019-10-13",
"UserId": "c963d785-59fc-4384-5e7d-d466=2118e3347",
"UserType": "Guest",
}
}
headers : {
"Content-Type": "application/json"
}
I found it odd that when I omit headers, the payload is seemingly OK, and it sees that Content-Type has not been supplied. Once I add it in, then suddenly payload is not OK.
If I individually perform the requests, all the same data, it's also fine (items create in SPO list without issue). I feel like this fact confirms that the issue is not with the fields in the body.
I have no issues when batching GET requests (e.g. batching 100's of requests for auditLogs/signIns). These are essentially the same payload, minus no body/headers, so just id, url, and method.
Has anyone had any experienced this and found a solution?
PS. The Guid's are all fakes.
My issue ended up being that I needed to use ConvertTo-Json's -Depth parameter to successfully capture all of my body's content. It was 5 levels deep (Requests: [ Each Request: { body: { fields: { field names/values } } } ]).
This was only an issue with POST method requests, since a body is then needed, which in this case was for adding SPO list items, which requires the fields: key with children names and values for the list's columns / cells.

Translate revit model to svf from bucket id

Im trying to translate a revit model to svf using this endpoint https://developer.api.autodesk.com/modelderivative/v2/designdata/job
According to the docs this endpoint needs at lest these two attributes to work:
urn
formats
First I got the objectId from the desired object inside my bucket :
"objectId":"urn:adsk.objects:os.object:gkwz1o52jxgqnqkhay5dlgcg7e2xglal_tutorial_bucket/rst_basic_sample_project.rvt"
and then I used https://www.base64encode.org/ to encode this string and get my urn, following the advice from this question how to get the urn of the bucket file. The result urn was this:
dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6Z2t3ejFvNTJqeGdxbnFraGF5NWRsZ2NnN2UyeGdsYWxfdHV0b3JpYWxfYnVja2V0L3JzdF9iYXNpY19zYW1wbGVfcHJvamVjdC5ydnQ=
Finally I got the format from the examples and I removed the = from the urn string.
My final body data is this:
{
"input": {
"urn": "dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6Z2t3ejFvNTJqeGdxbnFraGF5NWRsZ2NnN2UyeGdsYWxfdHV0b3JpYWxfYnVja2V0L3JzdF9iYXNpY19zYW1wbGVfcHJvamVjdC5ydnQ"
},
"output": {
"formats": [
{
"type": "svf",
"views": ["2d", "3d"]
}
]
}
}
But im getting this message:
I thought that was I getting the right object, and getting the urn, but still don't know what I'm doing wrong.
The endpoint URL, payload, and the base64-encoded object ID all seem to be fine. Try a couple of things:
Make sure that you that you're using POST method when calling the endpoint
Make sure that you're using the same Forge credentials (client ID and secret) you used when uploading the Revit model to the bucket
Try other ways of calling the endpoint, for example, using Postman or the VSCode extension

Is there any documentation for EasyPost that shows the raw JSON for the requests, including headers? Or e.g. a PostMan collection?

I'm just doing the preparation for an integration with EasyPost's Shipping API, which will be server side C#, but we always build a PostMan collection for new integrations, so that we can test data separately from the application if there's an issue.
While I do love the fact that EP provide C# libraries and examples, I'm struggling to find anything that just gives me a list of required headers and the raw JSON format for the body of any requests. It feels a bit like they're just being a little too helpful.
I'll be looking at the Orders endpoint probably.
I've got an account, I've checked all their documentation and searched the internet but haven't found anything so I'm hoping I'm not the first developer to want to use a client application for testing outside my code.
Update:
EasyPost now does have a public workspace https://www.postman.com/easypost-api
with at least 1 public collection
The curl examples are basically the same as json:
For the Address creation example:
-d "address[street1]=417 MONTGOMERY ST"
is the equivalent of
{ "address": { "street1": "417 MONTGOMERY ST" } }
(you might have to escape some characters to be valid json).
Check out How to stimulate cURL request to a request using postman for postman with HTTP Basic Auth.
EasyPost does not provide a public Postman collection; however, here is an example of a shipment Postman body (raw) that could be used. You can adjust the values, actions, objects, etc to your needs.
Using the following, you shouldn't need to pass any headers. You'll pass your API key under the username field with the Basic Auth authorization type.
{
"shipment": {
"to_address": {
"id": "adr_123..."
},
"from_address": {
"id": "adr_123..."
},
"parcel": {
"id": "prcl_123..."
},
"carrier_accounts": {
"id": "ca_123..."
},
"options": {
"address_validation_level": "0"
}
},
"format": "json",
"controller": "shipments",
"action": "create"
}

"status_code": 422, "error_code": "REQUEST_PARAM_PARSE", "detail": "The 'data' parameter must be a list"

I’m using the HTTP Request Wizard to set up a POST request to MonkeyLearn. This endpoint expects a JSON in the request body (or payload body). It must be an object with the data property and a list of the texts you want to classify as value. For example:
{
"data": [
"This is the best sentiment analysis tool ever!!!",
]
}
So, in the HTTP Request Wizard, I set up under Parameters:
Name: data
Value: "This is the best sentiment analysis tool evert!!!"
Type: GetorPost
However, in the preview response I get:
{
"status_code": 422,
"error_code": "REQUEST_PARAM_PARSE",
"detail": "The ‘data’ parameter must be a list"
}
I can’t figure out what list the endpoint expect?
I am not familiar with the "HTTP Request Wizard" and cannot find it either. But from what you've written it is clear that you are sending a string and not an array of strings. You value should look like this: Value: [ "This is the best sentiment analysis tool evert!!!" ]

Bad Request error while using google drive api to copy file into a folder

I am trying to use the google drive api to copy a file into a folder
As the documentation says i am generating a POST request to https://www.googleapis.com/drive/v2/files/{fileId}/copy?access_token={MY_ACCESS_TOKEN}
i am using python to generate the request and in order to specify the target folder i am setting the request body to -
{
"parents":
[
{
"id":"{parent_id}"
}
]
}
so the final code is like-
url = https://www.googleapis.com/drive/v2/files/Ansjd021-23123414kjn2k3jn2/copy?access_token=Ya.asdsakjn2312k3n21kj3n-12312j3knb123l
body = {
"parents":
[
{
"id":"ZXC12312323123-Blahasd12323X"
}
]
}
request2 = urllib2.Request(url, urllib.urlencode(body))
request2_open = urllib2.urlopen(request2)
response = request2_open.read()
request2_open.close()
print response
The problem being that I am getting a Http Error : 404 Bad Request
I cant seem to figure out the problem with this. I am doing the same thing using the google playground and its running there.
I did this using https://developers.google.com/drive/v2/reference/files/copy#try-it and got ...
POST https://www.googleapis.com/drive/v2/files/1234/copy?key={YOUR_API_KEY}
Content-Type: application/json
Authorization: Bearer ya29.AHES6ZSZB-t4b4HsR5
X-JavaScript-User-Agent: Google APIs Explorer
{
"parents": [
{
"id": "1234"
}
]
}
This looks pretty much the same as your sample, except the authtoken is in an http header rather than the URL. Also, what happens if you don't urlencode the body? Is there a longer error message in the response body?