aurelia-http-client connects to wrong address - xmlhttprequest

I have a problem with the aurelia Http Client.
My api (http//localhost:3000/api/posts) works fine. The output of a get call (in postman or in the browser) is:
[
{
"_id": "58a5f4f635c3ab643c74d97a",
"text": "Foo",
"name": "Fooo",
"__v": 0
},
{
"_id": "58a5fcc32586d0683455f78d",
"text": "Bar",
"name": "Baar",
"__v": 0
}
]
This is my get call in the aurelia app:
getPosts(){
return client.get('http//localhost:3000/api/posts','callback')
.then(data => {
console.log(data);
return data.response;
})
}
And this is the output:
As you can see in the image the response contains something with "Aurelia" but my api never touched aurelia so i think there is something wrong with the URL.
Update1:
The fix suggested by GManProgram (missing :) was the problem.
Update2:
I have changed to the client to the aurelia-fetch-client as GManProgram suggested.
Here is the new output
I seems to put the address from the api behind its own address. Ho can I force it only to use the api address?

So first things first, in the example you posted, you are missing the : character after http in the URL.
If that doesn't fix it, and you are using the HttpClient from aurelia-fetch-client, then you may want to try using the .fetch method instead of the .get method
http://aurelia.io/hub.html#/doc/api/aurelia/fetch-client/1.1.0/class/HttpClient
In your case, since it looks like you are expecting json, the typical fetch call would look like:
return this.httpClient.fetch('http://localhost:3000/api/posts')
.then(response => response.json())
.then(response => new CaseModel(response));
Where you can also import the json method from aurelia-fetch-client.
Otherwise, maybe the HttpClient has already been configured in the application with a base URL and it is screwing you up?

What about:
return client.get('posts','callback')

Related

how to specify certain field which is inside array in restapi

I have an api link https://apilink.com?_fields=id,name,images which gives me the following format
[
{
"id": 229210,
"name": "Basic Electrical Knowledge",
"images": [
{
"id": 229211,
"date_created": "2023-01-13T18:34:39",
"date_created_gmt": "2023-01-13T07:34:39",
"date_modified": "2023-01-13T18:34:39",
"date_modified_gmt": "2023-01-13T07:34:39",
"src": "https://sampleSite.in/wp-content/uploads/2023/01/SomeUrlSource.jpg",
"name": "Basic Electrical Knowledge",
"alt": ""
}
]
}
]
I want to access only src from images[]. How do I retrieve this from the link. When clicking the link I want to display this:
[
{
"id": 229210,
"name": "Basic Electrical Knowledge",
"src": "https://sampleSite.in/wp-content/uploads/2023/01/SomeUrlSource.jpg"
}
]
How do I do this?
I tried to solve this by providing this parameters:
https://apilink.com?_fields=id,name,images=src
You can achieve this by making a GET request to the API link and then using a library such as JSON.parse() to parse the response and extract the necessary data. After that, you can use a for loop to iterate over the 'images' array in the response and extract the 'src' key from each object in the array. Finally, you can construct a new object with the desired format and return it.
fetch(https://apilink.com?_fields=id,name,images)
.then(response => response.json())
.then(data => {
let newData = []
data.forEach(item => {
let newItem = {
id: item.id,
name: item.name,
src: item.images[0].src
}
newData.push(newItem)
});
return newData;
})
.then(newData => {
console.log(newData);
});
Note that this code snippet is simplified and doesn't handle errors, it's only serve as an example of how you could do it.
Assuming that you can't make server-side changes, implement a little script, and want the result just manipulating the URI the response is no.
The URI is referring to a resource in the server, the _fields seem like a projection to make to the attributes of the desired resource.
In this case, you are trying to make a transformation on the resource given by the server through. If the server does not implement such functionality you must do it by yourself.
You want to transform the attribute images that has type [Object] to a String.
A code snippet like the answered by #RASIKA EKANAYAKA would fit your requirement.

How to avoid caching for specific Nextjs api route?

I have nextjs api route /api/auth/signout, it basically just clears the specific cookie and send json back.
The problem is, when I deploy project in Vercel, this particular API working first time properly(cookie cleared and giving 200 response) but later it's not working (cookie not cleared and giving 304 response).
I want to know, is there any way to avoid cache only for this route?
What's the best possible way to fix this problem?
You can configure Cache-Control header per API endpoint
https://nextjs.org/docs/api-reference/next.config.js/headers#cache-control
In your case, something like this might do the trick:
res.setHeader('Cache-Control', 'no-store')
Added this config on next.config.js
async headers() {
return [
{
source: '/api/<route-name>',
headers: [
{
key: 'Cache-Control',
value: 'no-store, max-age=0',
},
],
},
];
},

Change custromer-request-type in Jira ServiceDesk via REST API

I can receive the values of an created ticket using the SD API like:
GET /servicedeskapi/request/SD-4532
and within that i find something like:
{
"issueId": "71928",
"issueKey": "SD-4532",
"requestTypeId": "121",
"serviceDeskId": "5",
...
}
whereas "requestTypeId" related to the type created by the user (e.g. has a label "Common question").
Now i want to change the request type to, let's say "Hardware issue" which have the requestTypeId of "89".
When i try to change by POST /servicedeskapi/request/SD-4532
and giving a payload of
{
"requestTypeId": "89",
}
I get a "405 - Method not allowed". Also the Jira ServiceDesk REST-API doc does not state anything about a POST method for this.
So i tried the common Jira REST-API
PUT /api/2/issue/SD-4532
and give payload
{
"fields": {
"customfield_10001": {
"requestType": {
"id": "89"
}
}
}
}
but that give me an "Field 'customfield_10001' cannot be set. It is not on the appropriate screen, or unknown." error.
The reason why the Jira ServiceDesk REST API docs do not state anything about a POST method for this because.... there is no POST method for this. You cannot change a request (issue) type simply by altering the value of the field that contains the ID (metadata) of that request type.
Do a Google search on "jira rest api change issue type" to see the many other times this question has been discussed in the past in many places. In a nutshell, changing types is not possible via the REST API, only the web UI.
Use Jira Rest API to update Jira issue information. https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-rest-api-3-issue-issueidorkey-put
// This code sample uses the 'node-fetch' library:
// https://www.npmjs.com/package/node-fetch
const fetch = require('node-fetch');
const bodyData = `{
"fields": {
"customfield_10010": "ABC-09",
"customfield_10000": {
"air": "",
"type": "doc",
"name": "Sample Process",
"avatarUrl": null
}
}
}`;
fetch('https://your-domain.atlassian.net/rest/api/3/issue/{issueIdOrKey}', {
method: 'PUT',
headers: {
'Authorization': `Basic ${Buffer.from(
'email#example.com:<api_token>'
).toString('base64')}`,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: bodyData
})
.then(response => {
console.log(
`Response: ${response.status} ${response.statusText}`
);
return response.text();
})
.then(text => console.log(text))
.catch(err => console.error(err));
email#example.com --> Your Jira emailid
<api_token> --> This is from your account token generated
Note:- Before using any API check if it's not experimental as this may cause issues in future due to sudden change in req/resp from Jira.
Try to inspect Jira dashboard network tab to understand more.

Getting Code 400 using Dialogflow on API request

this is my very first time using Dialogflow, so probably my mistake is very stupid.
here is my problem:
1) I created a sample agent "small-talk'.
2) I enabled the Webhook in the fulfilment section. I setup the URL of the web server making the request and the auth (username, password) of the that web server.
3) I uploaded a simple webpage on that web server with an API request that looks like this one below (this is the sample json referenced in their guide):
axios({
method: 'POST',
url: 'https://api.dialogflow.com/v1/query?v=20150910',
headers: {
'Authorization': 'Bearer ad7829588896432caa8940a291b66f84',
'Content-Type': 'application/json',
},
body: {
"contexts": [
"shop"
],
"lang": "en",
"query": "I need apples",
"sessionId": "12345",
"timezone": "America/New_York"
}
})
.then((response) => {
console.log(response);
})
.catch((error) => {
console.log(error);
})
I keep getting this error:
Cannot parse json. Please validate your json. Code: 400"
The only thing I can thing of, is that I noticed that Dialogflow is now working with the API V2 enabled by default in the agent settings and it seems there is no selection to V1 available anymore. But maybe this has nothing to do with my problem.
Thanks in advance!
Solved it!
In the json request, instead of
body: {...}
I replaced it with
data: {...}
Probably it was obvious, but I am an absolute newbie on these things!
By the way, Google has shutdown Dialogflow V1 starting from 12th July 2021 as per this URL - https://cloud.google.com/dialogflow/docs/release-notes#June_15_2021
In case you are getting http response code 400 (bad request), it means that it is time to migrate :-)

How to POST to a url? (create an issue using JIRA api)

I have found this answer in how to create an issue in jira via rest api?
POST to this URL
https://<JIRA_HOST>/rest/api/2/issue/
This data:
{
"fields": {
"project":
{
"key": "<PROJECT_KEY>"
},
"summary": "REST EXAMPLE",
"description": "Creating an issue via REST API",
"issuetype": {
"name": "Bug"
}
}
}
In received answer will be ID and key of your ISSUE:
{"id":"83336","key":"PROJECT_KEY-4","self":"https://<JIRA_HOST>/rest/api/2/issue/83336"}
Don't forget about authorization. I used HTTP-Basic one.
Which I believe describes how to create an issue via posting to a url.
The problem is, I have no clue how this is actually implemented.
How does one POST to a url?
Is this the same as PHP post?
Where is the data kept?
What language is this all written in?
Sorry for such a vague question, This is all just so brand new to me I don't even know where to start researching this >_< Any sort of concrete example would be really really helpful!
Thank you!
The data section is written in JSON format, which is simply a text representation of a data structure. It is indented for readability, but really could be shown as:
{"fields":{"project":{"key": ""},"summary": "REST EXAMPLE","description":"Creating an issue via REST API","issuetype":{"name": "Bug"}}}
To POST to a URL and create an issue, you need a server-side mechanism to first authenticate aginst Jira, then send the data using HTTP POST.
In PHP, you can use cURL to POST or GET, or file_get_contents() to GET.
PHP cURL doc is here:
http://php.net/manual/en/book.curl.php
For example, here's a PHP function to create a Jira issue (after authentication):
public function createIssue(){
/*
Issue types:
1: Bug
3: Task
5: Sub-task
*/
$out = false;
$this->method = "POST";
$this->url = "http://10.50.25.64:8080/rest/api/2/issue/";
$this->data = array(
"fields" => array(
"project" => array("key" => $this->projectKey),
"summary" => $this->summary,
"environment" => $this->environment,
"description" => $this->description,
"issuetype" => array("id" => $this->issueType),
)
);
if (!empty($this->assignee)) $this->data['fields']['assignee'] = $this->assignee;
if (!empty($this->labels)) $this->data['fields']['labels'] = $this->labels;
foreach($this->customFields as $key => $val){
$this->data['fields'][$key] = $val;
}
$issue = $this->execCURL();
return $issue;
}
The function execCURL() takes the PHP array ($this->data) and sends it using PHP cURL.
Hope any of this helps!