Malformed JSON in Batch when getting Unified Group images - json

I'm using request https://graph.microsoft.com/v1.0/$batch API to fetch group images and within batch, single image response contains "Content-Type":"image/pjpeg" and that breaks json parsing of the response (unexpected token X in the JSON...).
Request:
{
"requests": [
{
"id":"88aec8df-ea5e-4ad6-8534-3b1ac70693e2",
"method":"GET",
"url":"/groups/88aec8df-ea5e-4ad6-8534-3b1ac70693e2/photo/$value"
},
{
"id":"beeadb9d-02cf-4437-ab7d-7762a58e70a6",
"method":"GET",
"url":"/groups/beeadb9d-02cf-4437-ab7d-7762a58e70a6/photo/$value"
}
]
}
Batch response from Chrome console
Seems that Graph batch does not Base64 encode pjpeg images correctly to response json. Normal "Content-Type":"image/jpeg" images in batch works fine.
Did not find anything related to this from known issues.

For sanity purposes: this was a bug in the batch processing that has been addressed since.

Related

Next.js API Route mysteriously modifying JSON payload

For some reason, when I send JSON formatted data through Postman as raw text, I have no issues. When I send the exact same data through Postman as raw JSON (the difference should only be that the content-type header is application/json instead of application/text), I end up with my double quotes stripped and my strings switched to single quotes.
Original payload example (Postman sends this):
{ "id": "blahblahbloo", "time": "hammer" }
Unintended transformation (NextJS receives this):
{ id: 'blahblahbloo', time: 'hammer' }
To be clear, I get the exact same thing (which is what I expect) when I send as raw text via Postman:
// Postman sends this and NextJs receives this when set to raw text
{ "id": "blahblahbloo", "time": "hammer" }
I'm not explicitly doing anything to read the content-type and transform the data. My endpoints with this issue is a NextJS Dynamic Route: https://nextjs.org/docs/api-routes/dynamic-api-routes
Next.js API routes have a built-in bodyParser middleware that will parse the incoming request's body based on its Content-Type header.
From the API Middlewares documentation (emphasis is mine):
API routes provide built in middlewares which parse the incoming
request (req). Those middlewares are:
req.cookies - An object containing the cookies sent by the request. Defaults to {}
req.query - An object containing the query string. Defaults to {}
req.body - An object containing the body parsed by content-type, or null if no body was sent
Sending the payload as application/json will have the API route convert the req.body to a JavaScript object, hence striping the double-quotes.
While the bodyParser middleware is automatically enabled by default, you can disable it if you want to consume the body yourself.
// In the API route
export const config = {
api: {
bodyParser: false
}
}

Postman: POST request of nested JSON via form-data not working (while via raw-data ok)

I want to POST the following JSON-object via Postman:
{
"title": "test_title",
"date": "2021-12-31",
"attachments": [
{
"name": "test_attachment"
}
]
}
This works perfectly fine, when using Postman's raw input form for the request-body: I get a "201 Created"-response back.
However, when using the form-data to POST the data, I get the error "Invalid data. Expected a dictionary, but got str." (see also screenshot below) What am I doing wrong here? I tried all kind of other versions to enter the attachment-key:value pair but nothing worked so far
I managed to make it work! (note: I added some additional fields compared to the screenshot in question. See below for details:
You did nothing wrong.
If you want to make a request with json object, then you go with raw type (json) in postman.
If you want to upload file, then you use form-data
One more thing, status 201 means the request is succeed, your object has been created.
var express = require('express')
const multer = require('multer')
const upload = multer()
var app = express()
app.use(express.json());
app.post('/test',upload.none(), function (req, res, next) {
res.send(req.body)
})
app.listen(80, function () {
console.log('web server listening on port 80')
})
Above is a sample endpoint which works with both form-data and json , just do a post to http://localhost:80/test with both form data and raw json
you can see both will get parsed correclty
APIs are just abstraction , its like a function that takes in many attribute, how you parse it depends on the implementation ( how the api function is written) .
so answer is "Talk to the developer" on how the API is implemented and what it is supporting
I'm having issue in placing json into form format the way Daniel did in Postman. Need help in figuring out what is it required to place the cascaded json objects into form data format. Please see here that I'm trying to accomplish.
JSON Format (to be filled into Postman form-data section:
{
"primary_object": {
"child_object_1": [{"id": 12345678, "value": "abc"},{"id": 87654321, "value": "xyz"}],
"child_object_2": [
"first_val",
"second_val"
]
}
}

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

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.

freeradius 3.0.17 rlm_rest parsing json response

I'm trying to authenticate RADIUS Requests against a RESTful API (provided by Customer) using rlm_rest.
The problem I am facing is that
response JSON format (of REST API provided by Customer), is different from rlm_rest default format (indicated in etc/raddb/mods-enabled/rest).
My Virtual Server configuration as below:
Default
authorize {
...
...
rest
if (ok) {
update control {
Auth-Type := rest
}
}
}
mods-enabled/rest
authorize {
uri = "https://3rd-party-API/auth"
method = 'post'
body = 'json'
chunk = 0
tls = ${..tls}
data = '{
"code": 1,
"identifier": %I,
"avps": {
"User-Name": ["%{User-Name}"],
"NAS-IP-Address": ["%{NAS-IP-Address}"],
"Called-Station-Id": ["%{Called-Station-Id}"],
"Calling-Station-Id": ["%{Calling-Station-Id}"],
"NAS-Identifier": ["%{NAS-Identifier}"]
}
}'
}
Result
/sbin/radiusd -Xxx
HTTP response code
200
JSON Body
{
"code": "2",
"identifier": "91",
"avps": {
"Customer-Attributes": "Hello"
...
...
"Acct-Interim-Interval": "300"
}
}
The JSON structure is different from the example, and xlat parse
"code"
"identifier"
"avps"
And, of course, xlat finds no attributes match with the dictionary, while it cannot find "avps" and won't dig deeper.
So I was wondering is there anyway to either
Define the response JSON structure for xlat to parsing
Insert a "is_json" or "do_xlat" flag into the JSON ("avps"), and hope xlat will then dig deeper
Save the JSON and parse with exec/rlm_exec (using JQ or any other bash/JSON tools)
Please advise if there is any workaround. Thanks!
In FreeRADIUS version 4, there's a rlm_json module, which implements a custom node query language based on xpath (jpath), it is extremely limited and only supports some very basic queries (feel free to enhance it via PR :) ).
Below is an example I pulled out of my library of customer configurations. You can see here it's pulling out two keys (externalID and macAddress) from the root level of the JSON doc and assigning them to a couple of custom attributes (Subscriber-ID and Provisioned-MAC).
map json "%{rest_api:https://${modules.rest[rest_api].server}/admin/api/${modules.rest[rest_api].api_key}/external/getDeviceBySerialNumber?certificateSerialNumber=%{lpad:&TLS-Client-Cert-Serial 40 0}}" {
&Subscriber-ID := '$.externalId'
&Provisioned-MAC := '$.macAddress'
}
The xlat expansion can also be modified to send HTTP body data. Just put a space after the URL and pass your custom JSON blob.

Facebook API - Batch upload photos

I'm trying to batch upload photos and I keep getting a response back from Postman saying "Batch parameter must be a JSON Array" Does anyone see what is wrong with this because it looks like valid json to me. I've been rattling with this for a few hours making minor tweaks trying to get it to upload to no avail. I also have tried encoding the url.
https://graph.facebook.com/v2.11?batch=[{"method":"POST", "relative_url":"https://graph.facebook.com/v2.11/{pageid}/photos?access_token={access-token}", "body":"link_url":"https://{link-to-image}/wmphotos/999995/6d5cc4169bbf4e7dbe31f3739e025412/a572c29dff_640.jpg"}]&access_token={access-token}
Looking at your code, it seems that the JSON object in your batch array is actually malformed. body:link_url:link should be body: {link_url:link}
Bad:
[
{
"method":"POST",
"relative_url":"foo",
"body":"link_url":"link"
}
]
Good:
[
{
"method":"POST",
"relative_url":"foo",
"body":{"link_url":"link"}
}
]