Querydict not recognizing json array in django - ajax

Django and Django Rest Framework is not sensing the array in the following JSON object:
{
"datum":
[
{'proposed':'20/sep/2018', "pk":"475"},
{'proposed':'20/sep/2018', "pk":"517"}
]
}
When I do a print(request.data) this is the output:
<QueryDict: {'{"datum":[{"proposed_submission_date":"20/Sep/2018","pk":"475"},{"proposed_submission_date":"20/Sep/2018","pk":"512"}]}': ['']}>
and when I do a print(request.data.keys())I get:
{"datum":[{"proposed_submission_date":"20/Sep/2018","pk":"475"},{"proposed_submission_date":"20/Sep/2018","pk":"512"}]}
You can see that its taking the json string as the key, and not assigning "datum" as the key.
Do I need to do something else with the JSON string?
I'm doing an AJAX PUT to the Django rest framework backend.

the fact that you see a QueryDict rather than just a dict is a sign that you sent your data as application/x-www-form-urlencoded or multipart/form-data.
Ensure you send the request with a application/json content type and it should be just fine.

Related

Formats to use on a response with fast-json-stringify

What I want to do is add validation to the schema response from a fastify route.
Following the documentation from Fastify here we can see this
Ajv for the validation of a request
fast-json-stringify for the serialization of a response's body
Related to improve and add validations for a response, what I want to do is check the schema when I send a response.
fast-json-stringify support different options, included format, but if you read the documentation, they said that they support JSON schema. Jsonschema has support for email format, that you can see here as a built-in format but when I try to use it on Fastify, like this:
{
response: {
200: {
type: 'object',
required: ['email'],
properties: {
email: {
type: 'string',
format: 'email',
}
}
}
}
}
And try to return ad response this
reply.code(200).send({ email: 'test' })
The only validation that I can do is when I set the type to integer and try to return a string.
Did you know if it's possible to use ajv-formats with fast-json-stringify to add validation to the response schema and use the formats from ajv and add new ones?
Many thanks in advance!
fast-json-stringify does the serialization, not the validation.
The json schema provided to it will be used to serialize only the properties declared and some type checking like integer or arrays.
the enum keyword is not supported
the format keyword is supported only for the dates as documented:
To reach your goal, you should use this plugin: fastify-response-validation that will add a validation step of your response body before the serialization process.

Sending Array of string in grape API

Using grape api, I try to send an array of string but I always have the response error, codes is not valid
In my model, I expose my field like this :
expose :code, documentation: { type: Array[String]}
And when I make my test, I pass
"code": [
"037ffdb5-6901-404c-b152-8321a32aa397", "1762005c-8cf0-427d-b090-d354d6255eba"
],
When I check in my console, I have an array of string passed in my params :
=> ["037ffdb5-6901-404c-b152-8321a32aa397", "1762005c-8cf0-427d-b090-d354d6255eba"]
Is anyone having a clue why my params are not valid ?

Dynamics - Creating a Task using API

{Method: POST, RequestUri: 'https://cmpanydynamicsurl.com/api/data/v8.2/tasks', Version: 1.1, Content: System.Net.Http.StringContent,
Headers:
{
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json
Content-Type: application/json; charset=utf-8
Content-Length: 162
}}
Using the above request, I am trying to create a task against an account using Dynamics API post action.
Json Model being sent -
{
"subject":"NEW TEST TASK FOR ACCOUNT",
"_regardingobjectid_value#odata.bind":"/accounts(08b582ad-4e2f-e711-8101-5065f38a4a21)"
}
I am receiving this error message and have had no luck with searching the internet?
A property '_regardingobjectid_value' which only has property
annotations in the payload but no property value is declared to be of
type 'Edm.Guid'. In OData, only navigation properties and named
streams can be represented as properties without values.
The correct payload is
{
"subject": "NEW TEST TASK FOR ACCOUNT",
"regardingobjectid_account#odata.bind": "/accounts(08b582ad-4e2f-e711-8101-5065f38a4a21)"
}
You have to tell somehow to which object type you are binding, because regardingobjectid has mutliple types, and every type has its own separate relationship (in this case regardingobjectid_account). You are trying to bind value to a plain "Guid" property (because "_regardingobjectid_value" is a "Guid") but such property should be assigned exactly the same as you are doing with subject, so simply "_regardingobjectid_value": "08b582ad-4e2f-e711-8101-5065f38a4a21" but this will not work as you did not provide the type of entity.

Ruby on Rails - JSON parse error unexpected token

I read json file after load it into json but i got error JSON::ParserError unexpected token at, i couldn't json parse.Below i mentioned what i got output after file read
Here my code,
file = File.read("sample.json")
hash = JSON.load(file)
after read my json file,
"{\"rename\"=>[{\"from\"=>\"TTTC\", \"to\"=>\"AAAB\"}, {\"from\"=>\"AAAA\", \"to\"=>\"Description\"}, {\"from\"=>\"AAAC\", \"to\"=>\"test\"}], \"drop\"=>{\"fields\"=>[\"AAAG\", \"AAAH\"]}}"
This is not valid in json =>. JSON looks like
{ "rename": [{ "from": "TTTC" }] }
That's not JSON that's a string created by applying the inspect method to a hash.
You can convert it back to a hash with eval
hash = eval(file)
However eval can be a security hole, so you should only do this if you're confident about the source and contents of the file.
If you're encountering this error while testing - make sure to add .to_json to your request's body hash.
Example:
headers = {
'ACCEPTS' => 'application/json'
}
post '/api-endpoint-here', { name: 'Dylan Pierce' }.to_json, headers

API Blueprint - Use Data Strucutres in response, without Attributes

I'm using Apiary to mock out a new API.
I'm trying to avoid having to write out all the JSON responses over and over again. If I do that using a + Attributes(user) then it will auto generate a bunch of attributes blocks in the machine panel, which is super confusing in my mind (especially when you have multiple responses).
The resulting documentation looks way better if you write out the JSON request/response blocks manually.
Is there a way to store Request/Response objects as a Data Structure? Maybe a Model perhaps?
I'd love to be able to do something like this:
## Users [/auth]
A user object contains the these attributes.
+ Attributes (user) <!-- I like this here -->
### Refresh a token for a user [POST /auth/refresh]
+ Request (application/json)
+ Headers
Authorization: Bearer jsonWebToken
+ Response 200 (application/json)
+ Body
{
"data": [
(user) <!-- I wish this was the user data structure as JSON -->
],
"meta": {
"access_token": "jsonWebToken",
"token_type": "Bearer",
"expires_in": 3600
}
}
# Data Structures
## user (object)
+ id: 123 (number)
+ email: drew#funkhaus.us
Note: The user object is 30 attributes long in real life.
Unfortunately that's not a supported scenario, you can't but data structures into your JSON payloads.
So if I understand correctly - using Attributes is fine but you would like hide them in documentation. Could you confirm that?

Resources