Drupal 8-9 REST translations - rest

I can't send PATCH request to my REST API to send node with translations.
The error is 422 Unprocessable Entity: validation failed. title: Title: this field cannot hold more than 1 values.
I was trying JSON:API and REST, but the only way I found is to manually create translations, and after that update each translation separately.
The data I'm trying to PATCH to REST is
const data = {
'type': 'charset',
'title': [
{ 'value': sName1, 'lang': 'en' },
{ 'value': sName2, 'lang': 'ru' },
],
'field_unicode_range': [{ 'from': iFrom, 'to': iTo }],
};

The JSON:API does not seem to currently support translations (Nov 2021).
"does support PATCH requests on translations: updating translations is possible" source.
There does seem to be some work going on around this is this issue

Related

500 error when trying to create page using REST API

I'm currently using Confluence server and I'm currently getting a 500 error when I try to create a new page using the REST API. I am currently using an HTML macro that makes GET & POST requests using the Fetch API. I currently have no issues when making GET requests, only issues with POST requests.
I tried researching the error and saw someone mention that they fixed it by turning off collaborative editing in the space, but in my case that is not an option. Anyone have an idea of what is causing this error?
function createPage() {
let url = "http://exampledomain:8090/confluence/rest/api/content/"
fetch(url, {
method: 'POST',
headers: {
'Authorization': 'Basic USER:PASS',
'Content-Type': 'application/json',
},
data: {
'type': 'page',
'title': "New Page",
'ancestors': [{ 'id': 97059352 }], // parent page id
'space': { 'key': "EXAMPLE_SPACE" },
'body': {
'storage': {
'value': "<h1>New Page!</h1>",
'representation': 'storage',
}
},
}
})
.then(response => console.log(response))
.catch(err => console.log(err.message))
}
I see invalid data structure:
'representation': 'storage', <== extra comma
}
}, <== another extra comma
}
Also double check with your programming language that you can use single quotes (') and that they are correctly transformed into double quotes ("). JSON (Jira REST API) accepts only double quotes for keys and string values.

REST Call in MDriven, construct of HEADER?

I am trying to model the following REST call in MDriven
jQuery.ajax({
type: 'POST',
url: 'http://server.net/api/server/search.php',
data: {
'apikey': xxxxxx,
'apiversion': '3',
'action': "search",
'type':'near',
'lat':'59.91673',
'long': '10.74782',
'distance': '2000',
'limit': '10'
},
success: printJsonResponse,
dataType: 'json'
});
The ViewModel would as I understand it be as follows:
But it is hard to test if this is the "same" (and it doesn't work)
One way to see what is actually sent is to send your data to some echo service that just returns what it gets.
I think there must be REST services for this. I googled "Rest echo service" and got this for example:
https://postman-echo.com/get?foo1=bar1&foo2=bar2
Try to send your request to https://postman-echo.com and then write out the result.

How to post a new message on the IBM watson work services?

I'm trying to post with POSTMAN to the new IBM Watson work services but only get one error after the other.
This documentation does not clearly explain the body scheme for messages (or does it?):
https://workspace.ibm.com/developer/docs#genericannotation
So I'm guessing around:
POST to
https://api.watsonwork.ibm.com/v1/spaces/{spaceId}/messages
With headers:
Content-Type:application/json
Authorization:Basic 123456789
spaceId: MySpaceID
body:{"input": {"text": "Hello"}}
What's the right scheme for the body to post that message with success?
I always get the following error:
{
"timestamp": "2016-10-27T12:53:07.134+0000",
"status": 403,
"error": "Forbidden",
"message": "No message available",
"path": "/teams/{spaceId}/messages"
}
I create a Script on GitHub, you can have more details on file PostMessageOnWatsonWorkspace.py
I put part of the code below
appname = 'PostMessageOnWatsonWorkspace'
text = """
Visit [IBM site](http://www.ibm.com), and leave a *message*.
Have _fun_!!!
Code Line:
`code`
Code Block:
```
code block
```
Bye
"""
Currently to format has only Bold, Italic, Code and links.
And this is an example of Post data.
data = {
'type': 'appMessage',
'version': 1.0,
'annotations': [{
'type': 'generic',
'version': 1.0,
'color': '#4FC3F7',
'title': appname + ' --> sendRichMessage at ' + str(datetime.datetime.now()),
'text': text,
'actor': {
'name': 'Enio Basso',
'avatar': '',
'url': 'https://ebasso.net'
}
}]
}
Other details you can find on documentation.

Correct way to do a PATCH upsert request

What is the proper way to request the following upsert to a REST API?
I'm struggling to structure a NoSQL collection, which is based on the most requested returns to the front-end application.
Suppose you have the following document:
{
'user' : {
'private_comments': [
//object available only to user 1
{
'id': 1,
'bar': 'He is very good',
'...': '...'
},
//object available only to user 2
{
'id': 2,
'bar': 'He is very bad',
'...': '...'
}
],
'public_comments': '' //infos available to all users
}
}
It is needed to upsert an element to the user.private_comments array.
According to https://www.rfc-editor.org/rfc/rfc6902#appendix-A.5, I could request a replace instruction PATCHing the following data:
{ 'op': 'replace', 'path': '/user/comments/$index', 'value': 'Actually, he is OK'}
The problem is that '$index' is unknown in this case.
A possible solution that I came up with was to create something like the following operation:
{ 'op': 'upsert', 'find_by': 'id', 'find': 1, 'path': '/user/comments', 'value': 'Nope, he really sucks' }
However, the user implementing the API should't provide the id value inside the PATCH request, because this value is already accessible via the receive token. Should I simplify the operation to:
{ 'op': 'upsert', 'path': '/user/comments', 'value': 'Nope, he really sucks' }
and treat it at the backend, so when it's and upsert operation without 'find' and 'find_by' variables I assume 'find_by': 'id' and 'find': value_from_token?
Also, I cannot do a simple GET/UPDATE at the hole document because the user doesn't receive the hole document, so an update would compromise the data.

MongoDB, add new { field : value } in existing embedded document with multiple level dot notation?

What I am trying to do is add a new {field:value} for a blog post. So for example, if I wanted to start tracking impressions on websites.blog_posts.url: 'http://www.example.com/01.html' how can I add that impressions attribute for that blog post?
My current document structure:
{
email_address: 'webmaster#example.com',
password: 'random_password',
first_name: 'John',
last_name: 'Doe',
user_type: 'WEBMASTER',
newsletter: 'NO',
websites: [{
main_title: 'My Blog Website',
main_url: 'http://www.example.com',
blog_posts: [{
url: 'http://www.example.com/01.html',
title:'My first blog post',
description: 'My first description.'
}, {
url: 'http://www.example.com/02.html',
title: 'My second blog post',
description: 'My second description.'
}, {
url: 'http://www.example.com/03.html',
title: 'My third blog post',
description: 'My third description.'
}, {
url: 'http://www.example.com/04.html',
title: 'My fourth blog post',
description: 'My fourth description.'
}]
}]
}
Here is what I thought would work using update and making upsert TRUE.
db.my_collection.update( {'websites.blog_posts.url': 'http://www.example.com/01.html' }, {'$set': {'websites.blog_posts.impressions': 549}}, true )
The error that I received is:
*can't append to array using string field name [blog_posts]*
Maybe "$set" is not correct for this or maybe I can not reference that deep with dot notation? I just started using MongoDB yesterday, any help would be great.
Thank you!
What you're trying to do is not possible given your schema. Dot-notation can be multi level but if there's more than one level that is an array it can no longer be addressed using the positional operator '$'.
E.g. you'd need to do :
db.my_collection.update(
{'websites.blog_posts.url': 'http://www.example.com/01.html' },
{'$set': {'websites.$.blog_posts.$.impressions': 549}},
true );
But having two position operators in the update is not possible since MongoDB can only determine the position of an element in the first array.
Your only option is to redesign your schema to have a dedicated collection of user websites (which is better for other reasons too in this case).