Sends a request-body in a GET - rest

I'm trying to get data from API
I used curl to send a request body
curl -H "Content-Type: application/json" exemple.com/api/filter -XGET -d '{"param1": "aa"}'
and it's works, I've a data result but using axios it doesn't work
axios.get("exemple.com/api/filter", {data: {"param1": "aa"}}).then(response => {
return response.data;
}).catch(err => {
alert(err);
console.log(err);
})
There are any way to have an equivalent of curl -xGET to Axios ?
Thanks!

If this is run on the browser it will not work since:
The body argument provides the request body, if any, and is ignored if the request method is GET or HEAD.
https://xhr.spec.whatwg.org/#the-send()-method
However, if you running this on node engine it should work.
You should think about switching to axios.post to send json data to your api instead of get. If you are trying to get data then use query params axios.get(..., {params: {"param1": "aa"}}).

Related

Using Transactions with GraphDB REST API to push a file into a repository

i have the following requirement:
After selecting an ontology file i need to send it into my GraphDB-Repository. I´m programming with Angular 8.
I looked at the REST API Documentation and tried my best to understand what i need to do (i have like 3 months of experience with coding in angular [and overall actually], so all these concepts are still pretty new to me).
So i figured out i could start a new transaction, get the transaction-id and use it to submit my file. I dont know if this appraoch works, but anyway i´m not able to get the transaction-id, because it appears nowhere in the server response.
Recording to the documentation it should be in the header. But there is only the following content:
"cache-control": "no-store",
"content-language": "de",
"content-type": "text/plain;charset=UTF-8"
Thanks in advance!
var config_post: AxiosRequestConfig = {
method: `POST`,
headers: {
'Content-Type': 'application/json',
'Accept': 'text/plain',
},
// responseType: 'text',
// data: formData,
url: "http://localhost:7200/repositories/testdb/transactions"
}
Axios(config_post).then(function (response) {
console.log("Got a response from GraphDB ");
console.log(response.headers);
})
As Damyan mentioned, you don't need to start a transaction to upload a file. You can do something like:
curl -X POST -H "Content-Type: application/x-trig" --data-binary '#test_data.trig' 'http://localhost:7200/repositories/test/statements'
or
curl -X POST -H "Content-Type: application/x-trig" -T test_data.trig 'http://localhost:7200/repositories/test/statements'

Getting 404 error in Rest Api guest-cart. [GET] V1/guest-carts/{cart-id}

I am trying to get my created guest cart through the REST api after i create it with POST /guest-cart/ and use the returned ID.
when i try to get the created cart with
/rest/all/V1/guest-carts/{Cart-id}
It returns a 404 error
{ "message": "No such entity with %fieldName = %fieldValue", "parameters": { "fieldName": "cartId", "fieldValue": null },
This is also true when i try it with swagger
curl -X GET "http://website.com/default/rest/all/V1/guest-carts/{cart-id}" -H "accept: application/json" -H "Authorization: Bearer {token}"
in both cases I send it with the Authorization and content-type header
I tried making the call in Swagger and Postman
I expect to recieve an empty Guest-cart
See if you can get a response from the items call using GET - in your case, the response should have no items.
/V1/guest-carts/{cartId}/items
If this doesnt work, you may have other issues. Cart ID typically expires after 1 hour, so get a new Cart ID for this and future tests. Look into your Magento logs to see if any errors show up with additional info.

How to make a correct HTTP Post request with Meteor.js to Domino Datalab's Rest API

In my server side Meteor.js method, I'm trying to correctly make a request to Domino Data Lab's (DDL) Rest API.
DDL provides a platform for makes it possible to call a data science model via a REST API. Their documentation on this API is here:
http://support.dominodatalab.com/hc/en-us/articles/204173149-API-Endpoints-Model-Deployment
But, I doubt the documentation is helpful because I think an experienced Meteor developer will see the request examples in CURL or Python and know how to get the params correctly into the JSON format that DDL is looking for.
Domino Datalab provides the instructions for 4 methods, but not for Meteor.js. I'll post the examples for Curl and Python:
Examples
CURL Request
curl -v -X POST \
https://app.dominodatalab.com/MYURL \
-H 'Content-Type: application/json' \
-H 'X-Domino-Api-Key: YOUR_API_KEY' \
-d '{"parameters": [ "FOO", "BAR", "ETC"]}'
Python Request
import requests
response =
requests.post("https://app.dominodatalab.com/MYURL",
headers = {
"X-Domino-Api-Key": "YOUR_API_KEY",
"Content-Type": "application/json"
},
json = {
"parameters": ["FOO", "BAR", "ETC"]
}
)
print(response.status_code)
print(response.headers)
print(response.json())
I've tried a few different ways (using both the dataand paramsoptions) based on the documentation for Meteor, but here is my best try:
Meteor.methods({
score_app: function(){
var test = HTTP.call("POST", "https://app.dominodatalab.com/MYURL",
{ headers: {
"Content-Type": "application/json",
"X-Domino-Api-Key": "YOUR_API_KEY"
},
// This is where the problem is. Have tried multiple syntax versions and tried using the `params`options for the HTTP call instead of `data`
data: {'params': [143]
}
},
function (error, result) {
// The syntax below should be if not an error, log the result (for testing etc, otherwise, log "http post error". I may have incorrectly switched this around, but the original version I got from an online example had it the console.log statements in the reverse order.
if (!error) {
console.log(result);
} else{
console.log("http post error");
};
});
}
});
I've been using this entry in the Meteor documentation to send the parameters as a JSON object correctly:
http://docs.meteor.com/api/http.html
The connection to Data Domino Lab (DDL) is made correctly, but it doesn't recognize the parameters correctly because the request is not sending the parameters in the JSON format that DDL wants.
result: 'You must provide a JSON object in your request body
with a parameters key containing an array of parameters.' } }
I'm on the DDL free plan, but I will email a link to this question to their tech support. This is a niche issue, but it could be important to Meteor.js developers in the future wishing to link to a data science model in DDL.
I'm one of the engineers at Domino who has worked on the API Endpoints feature recently. The error
message you're getting means that the JSON object you're sending to our server doesn't contain the
key "parameters". I'm not an expert in Meteor, but it looks like you're using "params" where you
should use "parameters" in your JSON payload.
Around line 9 can you change...
{'data': {'params': [143]}}
to
{'data': {'parameters': [143]}}
If my understanding of your code is correct, that'll work correctly.
Cheers!

Return different MIME types based on Accept header parameter on Amazon AWS API Gateway

Could you explain how to setup Amazon AWS API Gateway to return different documents based on the request HTTP Accept header?
Two examples:
curl --request GET 'http://api.sample.com/v1/hello' --header 'Accept: text/HTML'
<html><body>Hello, World!</body></html>
curl --request GET 'http://api.sample.com/v1/hello' --header 'Accept: application/JSON'
{data:"Hello, World!"}
If you want API Gateway to act as a template rendering engine I don't think that will work but you could let your Lambda know which content-type the requester is looking for by passing the Accept header to your Lambda and let Lambda decide what to return.
You will need a mapping template(under Method Execution -> Integration Request -> Mapping Templates) for each Content-Type(data the requester is sending) you want to support.
A sample mapping template which takes the input from the request and transforms it into the JSON event so Lambda can work with it:
{
"headers": {
// maybe there is an easier way for Lambda to get this but I couldn't find it in the context object so I believe APIG needs to send it like this
"Accept": "$input.params('Accept')"
},
"message": "$input.params('message')"
}
Then in your lambda you can check the Accept header and send back a response based on that:
module.exports.handler = function(event, context) {
var msg = event.message.toUpperCase()
if(event.headers.Accept === 'text/html') {
return context.succeed('<html><body><h1>Transformed Message: ' + msg + '</h1></body></html>');
}
// all other requests get JSON...
context.succeed({transformedMessage: msg});
};
And the last step is back in API Gateway - under Method Execution -> Method RESPONSE -> Add Response. The Model can just be empty but set the Content-Type to text/html. This will tell API Gateway to let whatever you send back from Lambda pass through to any requests where Accept: text/html
EDIT: This answer assumed you are using Lambda as the backend but really the same idea could be applied to almost any backend service that you're using.

Is this how REST response is sent from server side (Yii2) to mobile side?

I'm building a REST-full API in Yii2 framework. I'm sending the data in JSON format and returning response in JSON response as well.
I'm making the test request with the call via cmd:
C:\Windows\system32>curl -v -H "Content-Type: application/json" -X POST http://<path_to_host>/www/users/register -d "{\"user_firstname\":\"Name\", \"user_lastname\":\"LastName\",\"user_email\":\"test#email.com\",\"user_username\":\"usernameTest\",\"user_password\":\"123456\",\"user_is_eighteen\":\"true\"}
with this call the corresponding action is called and my code is executed. Then at the end of the logic in the called controller I'm trying to send the response as the following:
.
.
creating variables $model and $error_msg
.
.
header('HTTP/1.1 200 OK ');
header('Content-type: application/json');
$response = Yii::$app->response;
$response->format = Response::FORMAT_JSON;
$response->statusCode = $200;
$response->data = [
'data' => $model,
'errors' => $error_msg,
];
Yii::$app->end();
so as said before, this I'm testing the call via cmd:
Later this request will be created (and responses will be received) by mobile application. This is what I got as a result from the cmd, and I'm wondering will this be enough for receiving the JSON response at the mobile side? (since I'm not sure how to test it)